Nullablity

P T Withington ptw at pobox.com
Thu Jun 22 14:36:16 PDT 2006


> From: Brendan Eich <brendan at mozilla.org>
> Date: 21 June 2006 12:10:58 EDT
>
> I'm sympathetic to this "don't make implicit option types by making  
> class types nullable" argument -- I made it in TG1 meetings a while  
> ago.  But before that, waldemar's drafts and the derived  
> JScript.NET and ActionScript languages made class types nullable by  
> fiat.  This was not just a bad precedent.  Nullability for class  
> type has two independent arguments:
>
> 1.  Mindshare from Java, C#, and other languages that include null  
> among the values of reference types.

Strongly agree with Shijun He that this is a canard.  These languages  
include null in their reference types not because the programmer  
needs it but because the language designer/compiler writer was lazy.   
If we are trying to move the language forward, we should take this  
chance to bite the bullet and correct this error.

[Anecdote: Curl initially permitted null in its reference types, but  
under pressure from me (from my experience on the Harlequin Dylan  
team) changed to a scheme of having to explicitly declare when a type  
was nullable.  Curl had a large body of existing code where overnight  
declarations of T went from meaning 'T or null' to strictly 'T'.   
Very little of the code had to be changed to use the new 'T?'  
declaration, and in nearly every case where the new declaration was  
required it was realized that there was previously a potential  
runtime error due to not checking for null.]

> 2.   The difficulty of initializing variables of non-nullable type  
> with a sound default value.
>
> It would be good to hear new counter-arguments.

When it's too difficult, you use T? and don't initialize it.  You can  
also permit:

   var t:T;
   ...
   t = new T;

so long as `t` is not used in `...`.  In compiled code, the compiler  
may be able to omit any runtime check if it can prove that through  
flow analysis, otherwise it needs to runtime check usages of `t`.

I strongly support having types _not_ include null and requiring  
explicit declaration of nullability.  [Dylan and Curl are two  
examples of optionally-typed dynamic languages where such a scheme  
has been successfully implemented.]




More information about the Es4-discuss mailing list