https://kotlinlang.org logo
Title
f

Florian

08/29/2019, 1:29 PM
Can I use any data type in a
when
statement?
:yes: 3
m

Marko Mitic

08/29/2019, 1:32 PM
any that has proper
.equals
function
k

karelpeeters

08/29/2019, 1:36 PM
And if it doesn't you can still use
in
.
f

Florian

08/29/2019, 1:42 PM
I can create types without
.equals
, right?
a

Alowaniak

08/29/2019, 1:44 PM
It'll always have an .equals; it's just that it will use object identity by default or however it's called
also if you create a
data
class (since you're talking about "any data type") it will have a "proper" equals
f

Florian

08/29/2019, 1:46 PM
OK right, but the
when
will compile?
In the first case
m

Marko Mitic

08/29/2019, 1:47 PM
yeah
f

Florian

08/29/2019, 1:48 PM
OK, thank you
But why is it that
when
is so much more flexible than
switch
? Surely the designers must have had a reason why they made switch so limited.
And now `when`can do pretty much anything
m

Marko Mitic

08/29/2019, 1:56 PM
java just copied C's switch, which was a standard at that time
it's hard to change it without breaking compatibility but they could have added their own
when
they are just kinda slow and bureaucratic with changing the language
f

Florian

08/29/2019, 2:00 PM
OK, I see
k

karelpeeters

08/29/2019, 2:17 PM
To be fair the Kotlin
when
is not that powerful either, other languages like Scala, C#, Rust and others have actual pattern matching.
f

Florian

08/29/2019, 2:19 PM
What does pattern matching mean here?
m

Mike

08/29/2019, 5:19 PM
And in JDK12, they are beginning the process of improving
switch
. Much closer to Groovy switch/Kotlin when. Details: https://www.oracle.com/technetwork/java/javase/12-relnote-issues-5211422.html#JDK-8192963 And in future versions, they intend to introduce forms of pattern matching.