|
|
|
|
|
by arkadiytehgraet
2903 days ago
|
|
Funny you should mention Kotlin; while I like the language a lot, I believe this particular feature would be of immense help in the following scenario: Imagine you have a sealed class Foo, with `class Bar(x: String) : Foo()` and `Baz() : Foo()`. Now, imagine you have a method, returning an object of type Foo: `fun foo(): Foo` And you want to pattern match on the result of this method: when(foo()) {
is Bar -> ...
is Baz -> ...
}
Now, the problem is: how do you access String field x in the first branch? The only way to do it now is to extract the methos call into redundant local variable, and then pattern match it instead of `foo()` directly as I did above.Now imagine Kotlin had that feature; then we could just do the following: when(foo = foo()) {
is Bar -> foo.x
is Baz -> ...
}
|
|