-
Notifications
You must be signed in to change notification settings - Fork 224
Description
In some situations, program invariants may establish that a variable is non-null in a way that cannot be made apparent to the type system. Kotlin, Swift, and Typescript all have some form of null assertion operator to handle this.
The postfix !
operator has been proposed for this, since it is a familiar idiom from other languages (see below).
This issue is for discussion of the operator, and possible alternative syntax. cc @Hixie @munificent @lrhn @eernstg
For reference, the operator in other languages:
Kotlin has the !!
operator, which throws if the argument is null
// Kotlin
var s : String? = null
s!!.length // Throws if null, then calls the method
s!! // Throws if null
Swift has both a !
(force unwrap) operator (throws if nil), and also an implicitly unwrapped type which causes an implicit unwrap on each use.
// Swift
var str : String?
str = "Hello, playground"
str!
str!.count
let strImplicit : String! = str
strImplicit.count // equivalent to strImplicit!.count
Typescript has a purely static null assertion operator.
var str : String | null;
str!
str!.length