Sebastian Brunner
09/23/2020, 6:46 PMif (1 <= x <= 10) // do something
This would greatly improve readability and would reduce bugs since it's easier to understand that x is within the boundaries of 1 and 10.
Way better than the current equivalent: if (1 <= x && x <= 10)
. The "&& x" is just boilerplate code here...
Assignments:
For assignments I'd like to have the possibility of assigning an assignment to another variable.
E.g. myObject.apply { inputFee = outputFee = 1.0 }
In this case both inputFee and outputFee would be set to 1.0 (or more like inputFee would be set to outputFee).
This is useful for mapping code so it makes most sense for properties.
I could also imagine like saving a property to a variable before it is modified in someway. Maybe like this:
val before = myObject.counter = someArg
myObject.count() // modifies counter
val difference = myObject.counter - before
What do you guys think?Guillermo Alcantara
09/23/2020, 6:53 PMval before = myObject.counter = 0
my mind tries to auto correct to val before = myObject.counter == 0
.Guillermo Alcantara
09/23/2020, 6:54 PMx in 1..10
araqnid
09/23/2020, 7:05 PMval before = myObject.counter = 0
How is the associativity working here? In the first example, you said inputFee
and outputFee
would both be initialised to 0. But in the second example, you seem to assume that myObject.counter
would be initialised to 0 and before
would get its previous value. So is a = b = c
interpreted as a = (b = c)
or (a = b) = c
? It seems reasonable to unroll before
in your second example, as it should always be 0.Sebastian Brunner
09/23/2020, 7:09 PMNir
09/23/2020, 7:13 PMaraqnid
09/23/2020, 7:13 PMval before = myObject.counter
myObject.counter = someArg
myObject.count() // modifies counter
val difference = myObject.counter - before
Especially if the compiler can’t make any guarantee about atomicity of myObject.counter, which would be the interesting thing here imho.Nir
09/23/2020, 7:13 PMNir
09/23/2020, 7:13 PMNir
09/23/2020, 7:14 PMNir
09/23/2020, 7:15 PMfun <T> T.exchange(new: T): T {
val old = this
this = new
return old
}
Nir
09/23/2020, 7:15 PMthis = new
can youSebastian Brunner
09/23/2020, 7:16 PMNir
09/23/2020, 7:17 PMSebastian Brunner
09/23/2020, 7:17 PMNir
09/23/2020, 7:17 PMNir
09/23/2020, 7:18 PMNir
09/23/2020, 7:18 PMbefore
to be the same as someargNir
09/23/2020, 7:19 PMbefore
to be the value of myObject.counter
, before someArg is assignedSebastian Brunner
09/23/2020, 7:20 PMsomeArg
.Sebastian Brunner
09/23/2020, 7:22 PMmyObject.counter
gets changed which doesn't affect before
since it's a primitive type.
So 1. set myObject.counter
to someArg
2. set before
to myObject.counter
which is now equal to someArg
Does this help?Sebastian Brunner
09/23/2020, 7:22 PMNir
09/23/2020, 7:31 PMSebastian Brunner
09/23/2020, 7:42 PMx in 1..10
doesn't quite cut it:
Assume you have two variables a
and b
, with this syntax you could easily compare them like: 1 <= a < b <= 10
.Sebastian Brunner
09/23/2020, 7:43 PMa
has to be smaller than b
.Nir
09/23/2020, 7:47 PMNir
09/23/2020, 7:50 PM1..10.containsAll(a, b) && a < b
Nir
09/23/2020, 7:50 PMNir
09/23/2020, 7:51 PMNir
09/23/2020, 7:51 PMSebastian Brunner
09/23/2020, 7:59 PMGuillermo Alcantara
09/23/2020, 7:59 PMval foo = var bar = 0
?Nir
09/23/2020, 7:59 PMNir
09/23/2020, 8:00 PMSebastian Brunner
09/23/2020, 8:00 PMSebastian Brunner
09/23/2020, 8:02 PMbar
) be val it could also implicitly be var...Nir
09/23/2020, 8:08 PMBen Woodworth
09/23/2020, 8:23 PMinline class Assign<out T>(private val value: Any?) {
inline operator fun component1(): T = value as T
inline operator fun component2(): T = value as T
inline operator fun component3(): T = value as T
inline operator fun component4(): T = value as T
//...
}
inline fun <T> assign(value: T) = Assign<T>(value)
And use it like this:
val (x, y, z) = assign(5)
I don't think you can use it to assign existing vars thoughNir
09/23/2020, 8:28 PMNir
09/23/2020, 8:31 PMfun <T> assign(t: T) = listOf(t, t, t, t, t)
Nir
09/23/2020, 8:31 PMBen Woodworth
09/23/2020, 8:31 PMNir
09/23/2020, 8:50 PMNir
09/23/2020, 8:51 PMvalue
is Any?