Can non-nullable references be implemented as a library?

steveh steveh57 at useshotmai.l
Sun Nov 7 09:21:49 PST 2010


Denis Koroskin Wrote:

> Since many people think that non-nullable references can be implemented as  
> a library and thus don't belong to core language, I've decided to show  
> that it is in fact impossible to do so.
> 
> How do you enforce the following behavior:
> 
> class Foo
> {
>      this()
>      {
> 	// error: variable nonNull not initialized
>      }
> 
>      this()
>      {
>          nonNull.someMethod(); // error: variable used before initialized
>          auto s = toString(); // error: can't call any methods before  
> initialized
> 
>          nonNull = new Bar();
>          s = toString(); // okay
>      }
> 
>      string toString() { return nonNull.toString(); }
> 
>      NonNull!(Bar) nonNull;
> }
> 
> class Bar : Foo
> {
>      this()
>      {
>          nonNull.someMethod(); // error: variable used before initialized
> 
>          super(); // initializes nonNull
>          nonNull.someMethod(); // fine
>      }
> }
> 
> Without support of these use-cases NonNull!(T) is useless.
> 
> There can be other examples, but I think these are enough to prove that  
> non-nullable references can not be implemented in library.

Andrei's stance is, either a library addon or ship D without that feature. D's library already contains both tuples and algebraic data types. They're simple to use, almost like in Python. The reason for library addons isn't that builtin features make less sense, the reason is that TDPL is already out and we can't improve the language in any radical way.


More information about the Digitalmars-d mailing list