Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

One-liner assignment methods #28

Open
voddan opened this issue Jul 1, 2016 · 4 comments
Open

One-liner assignment methods #28

voddan opened this issue Jul 1, 2016 · 4 comments

Comments

@voddan
Copy link

voddan commented Jul 1, 2016

How should a method be formatted which contains a one-line assignment? Use cases:

// mutating methods:
var x = 0
fun mutateX(d: Int) {
    x += d
}

// trivial custom property setters
var y = 0
    set(v) {
        y = v
    }

I want to openly discuss/vote on three solutions to choose the preferred one:

@voddan
Copy link
Author

voddan commented Jul 1, 2016

  1. One-liner:
fun mutateX(d: Int) { x += d }

@voddan
Copy link
Author

voddan commented Jul 1, 2016

  1. With lambda:
fun mutateX(d: Int) = run { x += d }

@voddan
Copy link
Author

voddan commented Jul 1, 2016

  1. Java-style:
fun mutateX(d: Int) {
    x += d
}

@voddan
Copy link
Author

voddan commented Jul 1, 2016

The current workaround is to fall back to explicit method invoketions instead of operators (for collections):

var arr = arrayOf(0, 0, 0)

fun inc1(i: Int) {
    arr[i] += 1
}

fun inc2(i: Int) = arr.set(i, arr[i] + 1)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant