Animesh Sahu
10/27/2020, 3:26 PMNir
10/27/2020, 3:30 PMVampire
10/27/2020, 3:30 PM=
in an if
like if (x = 1)
where it should have been if (x == 1)
. This can easily lead to bugs and can be prevented with assignments not being expressions.
The second guess is, that for method(x = 1)
it would be ambiguous whether you want to assign 1 to x and give 1 as first parameter, or whether you want to set the parameter x to 1.Animesh Sahu
10/27/2020, 3:32 PM(a = b) != c
to b.also { a = it } != c
Which is kotlinic, but not so readable.Animesh Sahu
10/27/2020, 3:32 PM:=
operator is implemented, I used them and this question ticked in my mind!Nir
10/27/2020, 3:44 PMNir
10/27/2020, 3:44 PMNir
10/27/2020, 4:15 PMif condition and (x := something):
...
Nir
10/27/2020, 4:15 PMx
. And obviously you can do that for arbitrary combinations of conditionalsAnimesh Sahu
10/27/2020, 4:16 PMAnimesh Sahu
10/27/2020, 4:16 PM(x := something) != null
Or something is required to make it Boolean and fit inside ifAnimesh Sahu
10/27/2020, 4:19 PMif ((x := b.toString()).isNotEmpty()) {}
Nir
10/27/2020, 4:21 PMNir
10/27/2020, 4:21 PMNir
10/27/2020, 4:21 PMNir
10/27/2020, 4:22 PMT?
based on condition
and whatever boolean expression on x
gildor
10/27/2020, 4:44 PMNir
10/27/2020, 6:44 PMNir
10/27/2020, 6:45 PMif (x := pattern1.match(s)):
...
elif (x := pattern2.match(s)):
....
etcNir
10/27/2020, 6:45 PMgroostav
10/27/2020, 7:44 PMmatch
is a type-check then kotlin gives you this with when
when(val x = expression(s)){
is Cow -> { } //x smart-cast to 'Cow'
}
But general pattern matching is still something all jvm languages --including java-- are wrestling with.Nir
10/27/2020, 7:47 PMNir
10/27/2020, 7:48 PMgroostav
10/27/2020, 7:48 PMgroostav
10/27/2020, 7:48 PMNir
10/27/2020, 7:48 PMNir
10/27/2020, 7:49 PMNir
10/27/2020, 7:49 PMregex_match(s) {
pattern1 to {
// use it
},
pattern2 to {
use it
},
}
Nir
10/27/2020, 7:50 PMNir
10/27/2020, 7:50 PMNir
10/27/2020, 7:50 PMgildor
10/27/2020, 11:20 PMNir
10/28/2020, 12:10 AMNir
10/28/2020, 12:11 AMNir
10/28/2020, 12:11 AMgildor
10/28/2020, 12:27 AMwhen (val foo = exp())
but for whole block),, it doesn't require making assignment an expressionNir
10/28/2020, 3:25 AMNir
10/28/2020, 3:26 AMNir
10/28/2020, 3:27 AMNir
10/28/2020, 3:27 AMNir
10/28/2020, 3:29 AMgildor
10/28/2020, 3:32 AMSpecial cases also have their own downsidesThis why important to see multiple real life cases when it required
Animesh Sahu
10/28/2020, 3:32 AMif (feature.isEnabled && System.getenv("test") != null) {
}
Somehow storing the result of getenvgildor
10/28/2020, 3:32 AMgildor
10/28/2020, 3:33 AMgildor
10/28/2020, 3:33 AMwhen
assignment (in when with assignment proposal), looks that it in general under consideration
But when it combined in if
it doesn’t look very well, though, it of course has one advantage, it limits scope of this function
So proposed something like
if (val test = System.getenv("test"); test != null) {
}
Animesh Sahu
10/28/2020, 3:37 AMval test = System.getenv("test") ?: return
if (feature.isEnabled) {
}
gildor
10/28/2020, 3:37 AMtest
local varaible is so much worse than messy code in ifgildor
10/28/2020, 3:37 AMAnimesh Sahu
10/28/2020, 3:37 AMgildor
10/28/2020, 3:37 AMgildor
10/28/2020, 3:39 AM=
instead of ==
Nir
10/28/2020, 3:44 AMNir
10/28/2020, 3:44 AMNir
10/28/2020, 3:45 AMNir
10/28/2020, 3:45 AMNir
10/28/2020, 3:46 AMNir
10/28/2020, 3:47 AMgildor
10/28/2020, 3:49 AM