Thread overview
Declare reference to variable ?
Jul 29, 2013
Temtaime
Jul 29, 2013
Namespace
Jul 29, 2013
Temtaime
Jul 30, 2013
Namespace
Jul 30, 2013
Namespace
Jul 30, 2013
Dicebot
Jul 29, 2013
bearophile
Jul 30, 2013
Maxim Fomin
Jul 30, 2013
yaz
Jul 30, 2013
yaz
July 29, 2013
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.
July 29, 2013
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;
July 29, 2013
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 ?
July 29, 2013
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
July 30, 2013
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.
July 30, 2013
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);
}
July 30, 2013
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();
}
July 30, 2013
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.
July 30, 2013
Oh, it doesn't work when accessing member data. Sorry for the noise.
July 30, 2013
Alternative:

----
import std.stdio;

struct S {
	uint longnamed;
	
	alias longnamed this;
}

void main() {
	S somestruct;

	alias v = somestruct;
	writeln(v);
	v++;
	writeln(v);
}
----