Suggestion: "Null Dangerous Type" with ?! operator

Null safety is a nice feature, but why not to make an option for a developer to opt out?

I mean language might have been extended with ?! type modifier, which would mean nullable but without null-safety checks:

var x: X?! = null
x.someMethod() //<!-- this is ok.

Yes, there is a !! operator, but it has to be used every time I am accessing the dangerous variable and it is quite anoying. Why not to make this descision at type delcaration moment – i.e. if a developer is absolutelly shure the variable will allways be initialized before access to it?

Isn’t it what lateinit vars are for?


Ups… Like it is, thank you!

1 Like

Seems like an unnecessary complication. Just assign to a new non-null variable. And if you verify it’s not null first, and the compiler knows the variable couldn’t have been modified between check and use, the compiler will allow you to treat it as non-null.

    var x: X? = null
    if (x == null) println("sorry, it's null")
    else x.someMethod() //<!-- this is ok.
1 Like