Why can't we make reference variables?

Tommi tommitissari at hotmail.com
Thu Aug 30 01:57:23 PDT 2012


On Thursday, 30 August 2012 at 07:35:34 UTC, Namespace wrote:
>
>> struct MyStruct
>> {
>>    // ref int defaultInitRef; // Illegal: reference variables
>>                               // can't be default initialized
>
> But you can handle it like const members: you have to 
> initialize these members in the ctor.

Yeah, maybe. I'm starting to think we should really know the 
implementation details of the language in order to even speculate 
about how ref might be implemented.

On Thursday, 30 August 2012 at 07:35:34 UTC, Namespace wrote:
> So ref Foo fr = null; is equally forbidden as
> [code]
> Foo f; // same as Foo f = null;
> ref Foo fr = f;

But you can always say:

Foo f = new Foo();
ref Foo fr = f;
f = null;

...after which fr references f, which references null, so 
effectively fr references null. I don't think this could be 
prevented from happening.



More information about the Digitalmars-d mailing list