Why can't we make reference variables?

Namespace rswhite4 at googlemail.com
Thu Aug 30 02:17:04 PDT 2012


On Thursday, 30 August 2012 at 08:57:24 UTC, Tommi wrote:
> 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.
I agree.

> 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.

That's true... Maybe in "ref Foo fr = f;" fr shouln't be a real 
pointer to f and instead an object which refer to the concrete 
object of f. Then it would be indifferent if you write f = null; 
because "fr" refers still to a valid object.



More information about the Digitalmars-d mailing list