Comment #0 by john.loughran.colvin — 2017-03-02T12:29:26Z
struct S
{
int a;
int b() { return a; }
@property int b(this X)() { return 2; }
}
pragma(msg, typeof(S.a)); // int
pragma(msg, typeof(S.b)); // int
pragma(msg, typeof(S.c)); // void
None of the expressions in the above typeofs can actually compile for real (need `this` reference for `a`). However, the top two seem to be allowed but the bottom isn't.
The makes it very painful to implement forwarding transparently (e.g. std.typecons.Proxy / Typedef) because if you template on `this` in order to handle shared/immutable/const then typeof expressions that worked for the original type won't work for the wrapped type. This leaves you with method duplication (x4: mutable, const, shared, shared const) as the only option.
Comment #1 by john.loughran.colvin — 2017-03-02T13:55:58Z
In order to add typeof(AggregateType.member) support to std.typecons.Proxy (with knockon improvements to a few other things in std.typecons), either this bug or https://issues.dlang.org/show_bug.cgi?id=17240 needs resolving.
Comment #2 by nick — 2022-09-14T13:12:49Z
> @property int b(this X)() { return 2; }
If you change b above to c (I assume it's a typo), you get:
int
int()
void
As c is a template, the type is supposed to be void. This is nothing to do with the template this parameter.
Comment #3 by nick — 2022-09-14T13:14:24Z
> As c is a template, the type is supposed to be void
This got added to the spec fairly recently:
> If the expression is a Template, typeof gives the type void.
https://dlang.org/spec/type.html#typeof
Comment #4 by john.loughran.colvin — 2022-09-14T19:26:27Z
(In reply to Nick Treleaven from comment #2)
> > @property int b(this X)() { return 2; }
>
> If you change b above to c (I assume it's a typo), you get:
>
> int
> int()
> void
>
> As c is a template, the type is supposed to be void. This is nothing to do
> with the template this parameter.
Yes, a typo.
I suppose that means this is a wontfix. typeof is weird, e.g.
typeof(a.b) c = a.b;
doesn’t actually work generically (aside from copy-ability concerns). I understand why it is this way, but it would be nice if the docs actually explained what it was supposed to do.
The type of an expression is… well it depends what that expression part of. The “Full Expression” section is not very enlightening.