Hacker News new | ask | show | jobs
by mike_hearn 3891 days ago
Much better than using static analysis is to have it in the type system:

https://kotlinlang.org/docs/reference/null-safety.html

It's simple:

val x: Foo = maybeGetSomeFoo() // Error: method returns Foo?

val x: Foo? = maybeGetSomeFoo()

x.sayHello() // Error: x may be null

x!!.sayHello() // OK, the !! method throws an exception if it's null

if (x != null) x.sayHello() // OK, flow sensitive typing means the test narrows the type

val h = x?.sayHello() // OK, ?. yields null if left hand side is null, otherwise evaluates right

val h = x?.sayHello() ?: return // OK, ?: runs right hand side only if left hand side is null

1 comments

"Much better than using static analysis is to have it in the type system"

I am not sure there is a distinction. Though it is true that some analyses may be run by a broader swath of users.