how can I do this ```when piece is { Pawn -> {}...
# getting-started
r
how can I do this
Copy code
when piece is {
Pawn -> {}
Bishop -> {}
...
}
as opposed to this
Copy code
if (piece is Pawn) {}
else if (piece is Bishop) {}
e
Copy code
when (piece) {
    is Pawn -> {}
    is Bishop -> {}
as documented… https://kotlinlang.org/docs/control-flow.html#when-expression
r
was hoping there was a less redundant way. but ill go with tis
🧌 1
3
a
How is this redundant?
v
How is this redundant?
He probably means repeating
is
k
If your
Pawn
,
Bishop
etc are
object
rather than
class
, you can leave out the
is
.
K 2
r
They're classes. You can have many instances of Pawns
He probably means repeating
is
yeah
k
you can have other predicates, right?
r
yeah so ideally I would like the
when
construct to support a parameterized expression
a way to define a function one time in the expression, then have when apply it implicitly
when (piece) apply( (type) -> piece is type { Pawn -> {} // implicitly do piece is Pawn Bishop -> {} }
c
Isn't that more verbose than the current code?
😄 1