Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 29, 2013 Declare reference to variable ? | ||||
---|---|---|---|---|
| ||||
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 Re: Declare reference to variable ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | 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 Re: Declare reference to variable ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | 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 Re: Declare reference to variable ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | 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 Re: Declare reference to variable ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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 Re: Declare reference to variable ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | 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 Re: Declare reference to variable ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | 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 Re: Declare reference to variable ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | 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 Re: Declare reference to variable ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to yaz | Oh, it doesn't work when accessing member data. Sorry for the noise. |
July 30, 2013 Re: Declare reference to variable ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | Alternative: ---- import std.stdio; struct S { uint longnamed; alias longnamed this; } void main() { S somestruct; alias v = somestruct; writeln(v); v++; writeln(v); } ---- |
Copyright © 1999-2021 by the D Language Foundation