Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 02, 2017 Imports incorrectly part of "allMembers" trait output? | ||||
---|---|---|---|---|
| ||||
When compiling the following code with DMD 2.072.2: class LeClass { import std.stdio; } void main() { foreach (memberName; __traits(allMembers, LeClass)) { pragma(msg, memberName); } } The following output is shown in the console: std toString toHash opCmp opEquals Monitor factory Note how "std" is part of the output of allMembers. Is this a bug or is this intended behavior? I don't think imports are necessarily members of a class. It also happens for static imports. |
January 03, 2017 Re: Imports incorrectly part of "allMembers" trait output? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Bierlee | On Monday, 2 January 2017 at 18:56:40 UTC, Mike Bierlee wrote:
> When compiling the following code with DMD 2.072.2:
>
> class LeClass {
> import std.stdio;
> }
>
> void main() {
> foreach (memberName; __traits(allMembers, LeClass)) {
> pragma(msg, memberName);
> }
> }
>
> The following output is shown in the console:
>
> std
> toString
> toHash
> opCmp
> opEquals
> Monitor
> factory
>
> Note how "std" is part of the output of allMembers. Is this a bug or is this intended behavior? I don't think imports are necessarily members of a class. It also happens for static imports.
It seems to be a bug that it takes std as there's no std as a part of object, not even as an import.
However there is: __traits(derivedMembers, T) which should only give you members of your actual class and not anything inherited from base classes and/or object.
|
January 03, 2017 Re: Imports incorrectly part of "allMembers" trait output? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Bierlee | given the way your code is written, "std" namespace *is* a member. it will be very useful when we'll fix other bugs and recursive `allMembers` scan will become possible. so while it came from namespaces implementation, i don't see this as a bug. it is little annoying, yes, 'cause you *have* to sort module ids out in your code. luckily, it is fairly easy, as module has no valid D type. |
January 03, 2017 Re: Imports incorrectly part of "allMembers" trait output? | ||||
---|---|---|---|---|
| ||||
Posted in reply to bauss | On Tuesday, 3 January 2017 at 06:23:01 UTC, bauss wrote:
> It seems to be a bug that it takes std as there's no std as a part of object, not even as an import.
there is. think of it as "ephemeral namespace entity" (kind of alias to "name bin" that compiler created, the entity that exists, just has no corresponding D type). as i wrote ealier, it will be useful eventually to walk import chains in CTFE.
|
Copyright © 1999-2021 by the D Language Foundation