Is `foo as? Foo` the full equivalent of `foo as Foo?` In kotlin?

Is it the foo as? Foofull equivalent foo as Foo??

If so, why are both needed?

If not, then what's the difference?

+4
source share
1 answer

as?is a safe broadcast operator .

Usually, if you try to use a variable and it fails, you get ClassCastException. Using this operator, it simply returns nullin this case.

This means that the return type of the expression foo as? Foois actually Foo?because it can return null.


foo as Foo? foo Foo?, , (.. foo null)).

+9

Source: https://habr.com/ru/post/1682382/


All Articles