www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Declare reference to variable ?

reply "Temtaime" <temtaime gmail.com> writes:
I have a long named variable in a struct.

For example let's name that longnamedstruct.longnamedmember

I need to use that variable in many places of the code and i 
cannot create the copy of it.

In c++ i can
auto &v = longnamedstruct.longnamedmember;

Now i can use v anywhere.
Why i cannot declare reference in D ? Or can i ?

I can make pointer to that, but it is workaround and it's need to 
use variable dereferencing by * operator.
Jul 29 2013
next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Monday, 29 July 2013 at 21:13:54 UTC, Temtaime wrote:
 I have a long named variable in a struct.

 For example let's name that longnamedstruct.longnamedmember

 I need to use that variable in many places of the code and i 
 cannot create the copy of it.

 In c++ i can
 auto &v = longnamedstruct.longnamedmember;

 Now i can use v anywhere.
 Why i cannot declare reference in D ? Or can i ?

 I can make pointer to that, but it is workaround and it's need 
 to use variable dereferencing by * operator.
You can use alias: alias v = longnamedstruct.longnamedmember;
Jul 29 2013
parent reply "Temtaime" <temtaime gmail.com> writes:
No, i cannot.

struct S {
	uint longnamed;
}

void main() {
	S somestruct;

	alias v = somestruct.longnamed;
	writeln(v);
}

Error: need 'this' for 'longnamed' of type 'uint'

Is it a bug ?
Jul 29 2013
next sibling parent reply "Namespace" <rswhite4 googlemail.com> writes:
On Monday, 29 July 2013 at 21:25:21 UTC, Temtaime wrote:
 No, i cannot.

 struct S {
 	uint longnamed;
 }

 void main() {
 	S somestruct;

 	alias v = somestruct.longnamed;
 	writeln(v);
 }

 Error: need 'this' for 'longnamed' of type 'uint'

 Is it a bug ?
Oh, that is annoying... Then, make your own reference type: import std.stdio; struct S { uint longnamed; } struct Ref(T) { public: T* ptr; disable this(); /* disable this(this);*/ disable this(typeof(null)); disable void opAssign(ref Ref!T); this(T* ptr) { this.ptr = ptr; } property ref inout(T) get() inout { return *this.ptr; } alias get this; } void main() { S somestruct; Ref!uint v = &somestruct.longnamed; writeln(v); }
Jul 29 2013
parent "Namespace" <rswhite4 googlemail.com> writes:
Alternative:

----
import std.stdio;

struct S {
	uint longnamed;
	
	alias longnamed this;
}

void main() {
	S somestruct;

	alias v = somestruct;
	writeln(v);
	v++;
	writeln(v);
}
----
Jul 29 2013
prev sibling parent "Dicebot" <public dicebot.lv> writes:
On Monday, 29 July 2013 at 21:25:21 UTC, Temtaime wrote:
 No, i cannot.

 struct S {
 	uint longnamed;
 }

 void main() {
 	S somestruct;

 	alias v = somestruct.longnamed;
 	writeln(v);
 }

 Error: need 'this' for 'longnamed' of type 'uint'

 Is it a bug ?
I'd say this is something in between bug and enhancement request. Nothing explicitly states that alias should work this way but it matches concept of alias much better than current behavior.
Jul 29 2013
prev sibling next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Temtaime:

 I have a long named variable in a struct.

 For example let's name that longnamedstruct.longnamedmember

 I need to use that variable in many places of the code and i 
 cannot create the copy of it.
You can shorten the outer name with an alias or "remove" it with a with() statement.
 Why i cannot declare reference in D ?
I don't know the reasons. But maybe you can create a little struct with just a pointer inside and an alias this to a member function that returns a ref. Bye, bearophile
Jul 29 2013
parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
On Monday, 29 July 2013 at 21:37:30 UTC, bearophile wrote:
 Temtaime:

 Why i cannot declare reference in D ?
I don't know the reasons. But maybe you can create a little struct with just a pointer inside and an alias this to a member function that returns a ref. Bye, bearophile
It doesn't work because alias this does not capture context pointer like delegate.
Jul 29 2013
prev sibling parent reply "yaz" <yazan.dabain gmail.com> writes:
You don't need to use dereference operator. Example:
http://dpaste.dzfl.pl/d34c23e5

import std.stdio;
struct Foo {
   void answer() {
     writeln("As you wish!");
   }
}

void main() {
   Foo veryVeryLongNamedStruct;
   auto v = &veryVeryLongNamedStruct;
   v.answer();
}
Jul 29 2013
parent "yaz" <yazan.dabain gmail.com> writes:
Oh, it doesn't work when accessing member data. Sorry for the 
noise.
Jul 29 2013