Can I use any data type in a `when` statement?
# getting-started
f
Can I use any data type in a
when
statement?
👌 3
m
any that has proper
.equals
function
k
And if it doesn't you can still use
in
.
f
I can create types without
.equals
, right?
a
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
OK right, but the
when
will compile?
In the first case
m
yeah
f
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
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
OK, I see
k
To be fair the Kotlin
when
is not that powerful either, other languages like Scala, C#, Rust and others have actual pattern matching.
f
What does pattern matching mean here?
m
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.