digitalmars.D.learn - Declare reference to variable ?
- Temtaime (10/10) Jul 29 2013 I have a long named variable in a struct.
- Namespace (3/13) Jul 29 2013 You can use alias:
- Temtaime (11/11) Jul 29 2013 No, i cannot.
- bearophile (8/13) Jul 29 2013 You can shorten the outer name with an alias or "remove" it with
- Maxim Fomin (3/10) Jul 29 2013 It doesn't work because alias this does not capture context
- yaz (13/13) Jul 29 2013 You don't need to use dereference operator. Example:
- yaz (2/2) Jul 29 2013 Oh, it doesn't work when accessing member data. Sorry for the
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
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
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
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
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
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
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
On Monday, 29 July 2013 at 21:37:30 UTC, bearophile wrote:Temtaime:It doesn't work because alias this does not capture context pointer like delegate.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
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
Oh, it doesn't work when accessing member data. Sorry for the noise.
Jul 29 2013