digitalmars.D.learn - alias this - am I using it wrong?
- Johann Lermer (33/33) Aug 25 2021 Hi all,
- jfondren (7/20) Aug 25 2021 line 17: ac.ts is copied into main's ts
- Adam D Ruppe (23/32) Aug 25 2021 What it does is if "a SOMETHING b" doesn't compile, it instead
- jmh530 (2/3) Aug 25 2021 That's a lot about alias this that I didn't know. Thanks.
- Johann Lermer (11/17) Aug 25 2021 On Wednesday, 25 August 2021 at 12:23:06 UTC, Adam D Ruppe wrote:
- FeepingCreature (14/47) Aug 25 2021 ts is a field. You can assign to a field. So when the field is
Hi all,
I have a little problem understanding alias this. I always
thought, that alias this only makes implicit conversions from the
aliased object to this. Then, why do lines 18 and 22 compile in
the code below? And, btw, line 22 crashes with a segmentation
fault.
```d
01 struct Test_Struct {long t;}
02
03 class Alias_Class
04 {
05 Test_Struct ts;
06 alias ts this;
07 }
08
09 class Test_Class
10 {
11 Alias_Class ac;
12 }
13
14 void main ()
15 {
16 auto ac = new Alias_Class;
17 Test_Struct ts = ac; // compiles
18 ac = ts; // compiles as well - why?
19
20 auto tc = new Test_Class;
21 ts = tc.ac; // compiles
22 tc.ac = ts; // again this compiles, but seg
faults
23 }
```
Johann
Aug 25 2021
On Wednesday, 25 August 2021 at 12:11:01 UTC, Johann Lermer wrote:
```d
14 void main ()
15 {
16 auto ac = new Alias_Class;
17 Test_Struct ts = ac; // compiles
18 ac = ts; // compiles as well - why?
19
20 auto tc = new Test_Class;
21 ts = tc.ac; // compiles
22 tc.ac = ts; // again this compiles, but seg
faults
23 }
```
line 17: ac.ts is copied into main's ts
line 18: main's ts is copied back into ac.ts
You can confirm this by changing t, doing the copy, and then
checking t.
line 21: segfaults because tc.ac is null, and you're trying to
copy tc.ac.ts into main's ts
Aug 25 2021
On Wednesday, 25 August 2021 at 12:11:01 UTC, Johann Lermer wrote:I have a little problem understanding alias this. I always thought, that alias this only makes implicit conversions from the aliased object to this.What it does is if "a SOMETHING b" doesn't compile, it instead tries "a.alias_this_member SOMETHING b" instead, or "a SOMETHING b.alias_this_member" instead if that's on the other side. The object with alias this must already exist though, so constructors are an exception (though another object's constructor can trigger some existing object's alias this when used as a param to that). The "SOMETHING" there can be operators like + or = or a .member. Only if both fail to compile do you actually get an error.17 Test_Struct ts = ac; // compilesSo what really happens here is the compiler sees ts = ac; fails to compile, so it is rewritten into "ts = ac.ts;"18 ac = ts; // compiles as well - why?So ac = ts fails, meaning it rewrites into `ac.ts = ts;`20 auto tc = new Test_Class; 21 ts = tc.ac; // compilesSo here it is rewritten into `ts = tc.ac.ts`.22 tc.ac = ts; // again this compiles, but seg faultsAnd now tc.ac.ts = ts; is the rewrite since the plain one didn't compile, thus accessing the null member. Note too that alias this can be to a function, in which case the rewrite will call the function. Implicit conversion isn't really what alias this is about. It kinda works (though note with a alias this struct it can be passed by value and thus copy, again the compiler just does the rewrite). It is just giving transparent access to a member.
Aug 25 2021
On Wednesday, 25 August 2021 at 12:23:06 UTC, Adam D Ruppe wrote:[snip]That's a lot about alias this that I didn't know. Thanks.
Aug 25 2021
On Wednesday, 25 August 2021 at 12:23:06 UTC, Adam D Ruppe wrote:
...
Thanks - that explains in all.
On Wednesday, 25 August 2021 at 12:23:32 UTC, FeepingCreature
wrote:
class Alias_Class
{
Test_Struct ts;
Test_Struct getter() { return ts; }
alias getter this;
}
Good idea, that should solve my current problem. The reason for
having an alias this was that I had a C data type
(cairo_surface_t) that I transferred into a class and I'm using
the alias so that I don't need to rewrite the whole application
at once. Unfortunately I overlooked some code that resulted in a
seg fault - and I was lost at understanding why.
Aug 25 2021
On Wednesday, 25 August 2021 at 12:11:01 UTC, Johann Lermer wrote:
Hi all,
I have a little problem understanding alias this. I always
thought, that alias this only makes implicit conversions from
the aliased object to this. Then, why do lines 18 and 22
compile in the code below? And, btw, line 22 crashes with a
segmentation fault.
```d
01 struct Test_Struct {long t;}
02
03 class Alias_Class
04 {
05 Test_Struct ts;
06 alias ts this;
07 }
08
09 class Test_Class
10 {
11 Alias_Class ac;
12 }
13
14 void main ()
15 {
16 auto ac = new Alias_Class;
17 Test_Struct ts = ac; // compiles
18 ac = ts; // compiles as well - why?
19
20 auto tc = new Test_Class;
21 ts = tc.ac; // compiles
22 tc.ac = ts; // again this compiles, but seg
faults
23 }
```
Johann
ts is a field. You can assign to a field. So when the field is
aliased to this, you can assign to the field through a class
reference.
You can disable this behavior by creating a getter in
Alias_Class, then aliasing it to this:
```
class Alias_Class
{
Test_Struct ts;
Test_Struct getter() { return ts; }
alias getter this;
}
```
Aug 25 2021









jfondren <julian.fondren gmail.com> 