struct init property

Jonathan M Davis jmdavisProg at gmx.com
Thu Aug 23 15:06:42 PDT 2012


On Thursday, August 23, 2012 23:27:18 Namespace wrote:
> My idea was to declare your own .init in a class, not in a struct.

Well, the init for classes will always be null, because class references are 
nullable. That's part of the language. Making it so that a class' init value 
wasn't guaranteed to be null would definitely complicate both the compiler and 
generic code (since it could no longer rely on the fact that a class' init 
value was null and would have to do additional checks when it cared). Right 
now, you have the guarantee that declaring a class reference allocates 
nothing, and changing init so that it could actually initialize the reference 
would break that guarantee. That would actually be a serious problem in cases
where you need to be able to declare a class reference but can't initialize it
when declaring it (e.g. a member variable, static variable, or global variable
must be known at compile time if it's directly initialized, which can't be done
with classes, since while you can sometimes use classes in CTFE, they can't
be persisted beyond CTFE).

If you want something that isn't nullable, you'll need a type which which 
isn't nullable, which means using a struct. I know that you want non-nullable 
references, but for D2, the best that you're going to get is a struct which 
wraps a class.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list