<@U0BLU7JTA> do you know if there are any examples...
# language-proposals
r
@dmitry.petrov do you know if there are any examples of that out there? I'd love to check it out.
d
Sorry, examples of what?
r
related to nullable expressions you mentioned:
if you can use optional assignment in conditions, you can use it in
when
for almost the same effect as unapply
I was wondering if there were examples out there of using nullable expressions in a similar way as unapply
d
Well, for example, in "good old" C++ null pointer is treated as false value, so "pattern matching" often looked like a cascade if:
Copy code
if (A* a = dynamic_cast<A*>(x))... 
else if (B* b = dynamic_cast<B*>(x))... 
else if (C* c = match_c(x))...
Similar idiom exists in various dynamic languages
So, in Kotlin it could look like
Copy code
when {
  val? a = x as? A ->... 
  val? b = x as? B ->... 
  val? c = C.unapply(x) ->... // ;)
}
It's a bit more explicit compared to Scala's unapply and F#'s active patterns, but also a bit more flexible: you can use just any expression returning nullable value
r
I see, thanks a lot!, I'm still thinking if there is a way even if it involves meta-programming to support matching on nested generics despite erasure. It's currently the feature I miss the most along with implicits from Scala
d
That would require additional runtime support, in the spirit of TypeToken
Or whatever Ceylon does with it's reified generics (practical solution would be to keep track of generic type information within an instance; however, I'd expect instanceof operation implemented on top of that to be rather slow, because instead of VM primitive you would have to use your own subtyping algorithm for your own runtime representation of types)
r
makes sense, thanks for the insight