state of an object

Jonathan M Davis jmdavisProg at gmx.com
Mon Jul 2 02:35:50 PDT 2012


On Monday, July 02, 2012 10:41:03 Namespace wrote:
> My intention was to avoid something like this:
> 
> [code]
> class Foo { }
> 
> Foo f; // f is null
> 
> NotNull!(Foo) test = f; // should throw an compiler error,
> because f is null
> [/code]
> 
> I can avoid
> 
> NotNull!(Foo) test = null; with
> 
> @disable
> this(typeof(null));
> 
> but how can i avoid null objects?

The _only_ way to statically disallow assigning a null Foo to a NotNull!Foo 
object is to statically disallow assigning a Foo to it period. Its nullity is 
not part of its type and therefore cannot be checked statically. As far as I 
can tell, you have two choices:

1. Throw when a NotNull is assigned or constructed from a null Foo.

2. Disallow assignment and construction of a NotNull!Foo from any and all Foos 
- in which case the NotNull!Foo needs to construct any Foo that it contains.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list