questions on nullability

Nicolas Cannasse ncannasse at motion-twin.com
Fri Jun 16 14:42:42 PDT 2006


> It is (see http://developer.mozilla.org/es4/spec/chapter_6_types.html), 
> but I don't know if the type Null (as opposed to value null) is 
> addressable. It isn't in AS3. Since the Null type has only one member, 
> there are currently no occasions where you would need it.
> 
> Peter

The docs say "A type is a set of values" so I guess they're talking 
about runtime types there. Types at compilation have a different 
meaning. In particular, nullity should be threated more like a 
constraint than a separate type since it can be toggled.

ML-style languages for instance encode nullity by using an "option type".

type option<T> = Null | Value : T

Null is option<*>
Value(0) is option<Int>
Value("") is option<String>

In order to extract the value from an option, you have to check it 
first, for example by using pattern matching :

var x = Value(0);
match x {
Null -> // handle null
Value(n) -> // n is the Number
}

With this kind of technics, it's waranty that there will *never* be any 
kind of null pointer exception.

Traditionaly OO language are using "null" a lot more, in particular 
because they don't enforce variable initialization upon declaration. So 
it's more often that we see null or not-null contraints in OO.

There are more infos there :
http://developer.mozilla.org/es4/proposals/nullability.html

Nicolas



More information about the Es4-discuss mailing list