is(x = module) vs. __traits(isModule, x)

Adam D. Ruppe destructionator at gmail.com
Wed Oct 7 20:29:07 UTC 2020


On Wednesday, 7 October 2020 at 20:01:53 UTC, Steven 
Schveighoffer wrote:
> If you want to determine if a given symbol is a module. Is 
> there another way?

A module symbol should *only* be given through means specific to 
modules. Which means the code knows it is dealing with a module 
without any introspection.

So like __traits(getImports) would ONLY return modules. 
__traits(allMembers) would NEVER return modules.

That said I guess I can see the point for completeness' sake to 
make __traits(isModule, mixin(__MODULE__)) return true.... but I 
don't see why you'd ever use that since it would be obvious in 
the code itself.

Perhaps useful if you get an aliased module though.

> Yeah, that's a problem we should fix. There was an 
> almost-released fix which made __traits(allMembers, mod) return 
> the full name of the modules, which I don't think was right 
> either.

That would be slightly less bad than it is now (a module should 
ALWAYS be referred to by its full name) but it is still the wrong 
place to put it.

D modules do not have parents. They may be associated with a 
package, but the package is not its parent in the strict sense 
since you can compile them independently.

> Not sure what this was supposed to mean.


Check this out:

```
module wtf.useless;

static if(is(wtf.useless MOD == module))
static if(is(__traits(parent, wtf.useless) PKG == package))
         static assert(0); // NOT TRIGGERED!
```

Yet:

// this prints "package wtf"
pragma(msg, __traits(parent, wtf.useless).stringof);


And:

module wtf.useless;

pragma(msg, wtf.stringof); // works


Yet:

static if(is(wtf.useless MOD == module))
static if(is(wtf PKG == module))
         static assert(0);

Error: module wtf.useless from file wtf.d must be imported with 
'import wtf.useless;'

(notice no line number either)

Unless you make wtf/package.d. Then it thinks it is a module 
instead of a package.


Oh and check this out:

pragma(msg, wtf.useless.stringof); // module useless


Good luck `import useless;`. Obviously won't work. It is right 
now *impossible* to get a module name back in the general case.

tbh, I don't even know what dmd is doing here. There seems to be 
no logic to it whatsoever.

If the language is going to pretend modules are children of 
packages (which they aren't and it shouldn't!!!!!), it should at 
least do so consistently. As it sits now, stuff that was possible 
a couple versions ago is now just hopelessly broken now.


More information about the Digitalmars-d mailing list