Bug 12223 – __traits(getMember,...) needed for aliases
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-02-21T22:21:27Z
Last change time
2022-08-22T11:52:12Z
Keywords
pull
Assigned to
No Owner
Creator
Vladimir Panteleev
Comments
Comment #0 by dlang-bugzilla — 2014-02-21T22:21:27Z
Currently, "x.y" does two things:
1. Looks up "y" in the symbol scope of "x"
2. Interprets "y" with its "this" set to "x"
Sometimes, it is useful to separate these two things.
One can already do 1 without 2 using alias:
alias y = x.y;
However, there is no way to do 2 without 1.
The closest thing we have is __traits(getMember, x, y) - however, it requires that y is a string, and does not work if it is an alias.
Example:
////////////////////////////////// test.d //////////////////////////////////
static template T(alias v)
{
void set() { v = 42; }
}
struct S
{
int i;
alias t = T!i;
}
void main()
{
alias f = S.t.set;
// ...
S s;
// Goal: call f() with "this" set to s, like this:
s.t.set();
// ... (but without referring to "t" or "set" directly)
//s.f(); // NG
//with (s) f(); // NG
//struct W { S s; alias s this; alias wf = f; }
//W w = W(s); w.wf(); // NG (Issue 12222)
//__traits(getMember, s, f)(); // NG
//__traits(getMember,
// __traits(getMember, s, __traits(identifier, __traits(parent, f))),
// __traits(identifier, f))(); // NG
}
////////////////////////////////////////////////////////////////////////////
I propose adding a __traits(child, x, y), since it will be the reverse operation of __traits(parent, y).
Comment #1 by andrej.mitrovich — 2014-02-22T00:49:49Z
That's a rather complicated example. Is the following what you're after?
-----
struct S
{
void foo() { }
}
void main()
{
alias f = S.foo;
S s;
__traits(getChild, s, f)(); // same as s.foo()
}
-----
Comment #2 by dlang-bugzilla — 2014-02-22T00:51:10Z
Yes, but it should also work with the original example (if the child is an alias of a symbol that is not actually declared in the parent, but the "this" pointer of which is nevertheless of the type of the parent).
Comment #3 by dlang-bugzilla — 2014-02-22T11:57:25Z
@rmanthorpe created dlang/dmd pull request #11442 "Issue 12223 - __traits(getMember,...) needed for aliases" mentioning this issue:
- Issue 12223 - __traits(getMember,...) needed for aliases
Add `__traits(child, p, c)` to interpret `c` with the `this` context set to `p`.
This is the opposite of `__traits(parent, c)`. `c` should be a symbol mising it's
`this` context, such an an alias to a member of `p`.
https://github.com/dlang/dmd/pull/11442
Comment #5 by dlang-bot — 2020-08-01T07:11:26Z
dlang/dmd pull request #11442 "Fix Issue 12223 - __traits(getMember,...) needed for aliases" was merged into master:
- da1a31509f43547a89fd7cd694499548ba345ee0 by Richard Manthorpe:
Issue 12223 - __traits(getMember,...) needed for aliases
Add `__traits(child, p, c)` to interpret `c` with the `this` context set to `p`.
This is the opposite of `__traits(parent, c)`. `c` should be a symbol mising it's
`this` context, such an an alias to a member of `p`.
https://github.com/dlang/dmd/pull/11442
Comment #6 by dlang-bot — 2021-01-03T15:41:40Z
@ibuclaw created dlang/dmd pull request #12093 "[dmd-cxx] Backport more recent traits to the C++ port" mentioning this issue:
- [dmd-cxx] Fix Issue 12223 - __traits(getMember,...) needed for aliases
https://github.com/dlang/dmd/pull/12093
Comment #7 by dlang-bot — 2021-01-03T22:57:05Z
dlang/dmd pull request #12093 "[dmd-cxx] Backport more recent traits to the C++ port" was merged into dmd-cxx:
- 1474ca231a6e9cf6a76c918661495242464b646e by Richard Manthorpe:
[dmd-cxx] Fix Issue 12223 - __traits(getMember,...) needed for aliases
https://github.com/dlang/dmd/pull/12093
Comment #8 by razvan.nitu1305 — 2022-08-22T11:52:12Z