Aquivalent References as in C++?

Namespace rswhite4 at googlemail.com
Mon Apr 16 14:50:20 PDT 2012


This is what I am actually doing at the moment, but I thougth 
that in a modern language like D, it is possible to have non-null 
references to avoid such constructs with assert.
My last hope was that an explicit ref would allow me both: 
non-null references _and_ lvalues, at least for objects.

That is very sad. Any possibility that this might change?
I really like that objects are implicit references and so i would 
not use structs, but it looks like i have no choice if i would 
avoid asserts..

On Monday, 16 April 2012 at 21:34:34 UTC, Timon Gehr wrote:
> On 04/16/2012 11:25 PM, Namespace wrote:
>> Hi,
>>
>> I have a few questions about D and could use some help. For
>> instance, how can i rebuild such a behavior?
>>
>> class Foo {
>> public:
>> Foo(const Bar& b) {
>>
>> At C++ you can ensure that a reference is requested and must 
>> not
>> null.
>> Nevertheless lvalues are functional, which means Foo(Bar(42))
>> would operate just as good as
>> Bar b(42);
>> Foo(b);
>>
>> The same procedure doesn't seem to operate with const ref,
>> instead you can only use ref. But in this case Ivalues doesn't
>> operate.
>> Because of the fact that in D all objects are indication of
>> references I thought that I simply didn't need a storage class.
>> But then I recognized that at my D equivalent…
>>
>> class Foo {
>> public:
>> this(Bar b) {
>>
>>
>> …null can also be passed.
>>
>> That case I would like to prevent, but at the same time allow
>> Ivalues. How does that work in D classes?
>> As far as I know this operates with structs, but shouldn't it 
>> be
>> possible with classes and objects too?
>>
>> Would be really nice if anyone had an advice for me.
>
> You could do this:
>
> this(Bar b)in{assert(b !is null);}body{




More information about the Digitalmars-d-learn mailing list