5 ms·
In Kotlin you can have a similar behavior but it's explicit which I like more. var a : T; // cannot be null var b : T?; // can be null a.something; // OK b.
by threatripper 4y ago
In Kotlin you can have a similar behavior but it's explicit which I like more.
var a : T; // cannot be null
var b : T?; // can be null
a.something; // OK
b.something; // not possible because b could be null
b!!.something; // assert that b is not null, then do the stuff
b?.something; // if b is null, return null, else do the stuff
So, each time you see the "?." you know to take extra care with that statement. On the other hand it's very short and easy to write chains of function calls that could return null which then falls through.
- happytoexplain 4y agoNote that this is all exactly how Swift works (though it uses one exclamation mark instead of two). The author is doing an extremely limited comparison, and simply doesn't mention these features.
- HNHatesUsers 4y ago
- Cloudef 4y agoNot sure if it's some runtime mode in kotlin but b?.something always threw panic for me when writing kotlin plugins for flutter, so I ended up having to guard the expressions with ifs anyways...
- seabrookmx 4y agoC# too.