References in D

Henning Pohl henning at still-hidden.de
Sat Sep 15 08:29:26 PDT 2012


On Saturday, 15 September 2012 at 14:36:33 UTC, Maxim Fomin wrote:
> On Saturday, 15 September 2012 at 14:17:53 UTC, Simen Kjaeraas 
> wrote:
>>> And in real code it could crash after dereferencing and 
>>> before passing to bar, if accessed (meaning that problem is 
>>> in erroneous pointer usage, not accepting ints through 
>>> reference in bar). Certainly, it is possible to break even in 
>>> such cases, as well as in other parts of the language which 
>>> doesn't necessarily mean that such parts of the language are 
>>> broken. What I was talking about is:
>>>
>>> void bar( ref int n ) {
>>>    n = 3;
>>> }
>>>
>>> void main( ) {
>>>    int* p = null;
>>>    bar(p); // error
>>>    bar(null); //error
>>> }
>>
>> But that's like saying bar(int n) doesn't accept null as a 
>> parameter - of
>> course it doesn't - int and int* are different types.
>>
>> What I'm saying is references may be a bit safer than 
>> pointers, but the
>> 'guarantee' that they're not null pointers in disguise is a 
>> lie.
>
> Again, what is the talk about? References in function 
> parameters or class objects? In the first case ref parameter 
> has nothing to do with whether was a valid object passed to 
> function or not (as like in your case). It only means that 
> function works with actual object, not a copy. Obviously, it is 
> impossible to pass a pointer or null when other type is 
> expected.
>
> If you speaking about class objects, than certainly it may 
> reference a valid region of memory or null (in bad and rare 
> cases, to random memory).

It is just about references to class objects.

Finally, will references to class objects in D stay nullable in 
future versions (D3)? What do you think?


More information about the Digitalmars-d mailing list