Thread overview
traits compiles does not work for symbols from other modules
Jul 24, 2017
Andre Pany
Jul 25, 2017
ag0aep6g
Jul 25, 2017
Andre Pany
Jul 25, 2017
Andre Pany
July 24, 2017
Hi,

I want to validate whether a class contains a specific attribute. I have the attribute name as compile time string. This string could either be a direct attribute of the class or a hierarchy (TextSettings.Font.Size).

As example T is the class Label and p.name contains the text "TextSettings.Font.Size".
TextSettings and Font are again classes.

mixin(`static if (__traits(compiles, `~fullyQualifiedName!T~`.`~p.name~`)) {...}

__traits(compiles, ...) only works for symbols of the same module, but not for symbols contained in other modules. I reduced it to this example coding:

m1.d
-----------------
module m1;

import m2;

class Foo
{
	int foo;
}

void main()
{
	static assert(__traits(compiles, m1.Foo.foo));
	static assert(__traits(compiles, m2.Bar.bar));
}

m2.d
---------------
module m2;

class Bar
{
	int bar;
}

Why does the second assert throws? What can I use instead of __traits(compiles,...) ?

Kind regards
André
July 25, 2017
On 07/24/2017 11:40 PM, Andre Pany wrote:
> m1.d
> -----------------
> module m1;
> 
> import m2;
> 
> class Foo
> {
>      int foo;
> }
> 
> void main()
> {
>      static assert(__traits(compiles, m1.Foo.foo));
>      static assert(__traits(compiles, m2.Bar.bar));
> }
> 
> m2.d
> ---------------
> module m2;
> 
> class Bar
> {
>      int bar;
> }
> 
> Why does the second assert throws? What can I use instead of __traits(compiles,...) ?

Works for me. What compiler are you using?
July 25, 2017
On Tuesday, 25 July 2017 at 08:30:43 UTC, ag0aep6g wrote:
> Works for me. What compiler are you using?

I reduced the example too lot. The issue is occuring if there is also a package.d is involved.

m1.d
-----------------------

module m1;

import sub; // Does not throw if replaced with: import sub.m2;

class Foo
{
	int foo;
}

void main()
{
	static assert(__traits(compiles, m1.Foo.foo));
	mixin(`static assert(__traits(compiles, sub.m2.Bar.bar));`);
}

sub/m2.d
----------------------

module sub.m2;

class Bar
{
	int bar;
}

sub/package.d
----------------------

module sub;

public import sub.m2;


I use DMD master (as I already need the static foreach) on windows 10.
DMD32 D Compiler v2.075.0-rc1-master-af3eacf

>> dmd -run m1.d sub/package.d sub/m2.d

Kind regards
André
July 25, 2017
On Tuesday, 25 July 2017 at 11:34:23 UTC, Andre Pany wrote:
> On Tuesday, 25 July 2017 at 08:30:43 UTC, ag0aep6g wrote:
>> Works for me. What compiler are you using?
>
> I reduced the example too lot. The issue is occuring if there is also a package.d is involved.
>
> m1.d
> -----------------------
>
> module m1;
>
> import sub; // Does not throw if replaced with: import sub.m2;
>
> class Foo
> {
> 	int foo;
> }
>
> void main()
> {
> 	static assert(__traits(compiles, m1.Foo.foo));
> 	mixin(`static assert(__traits(compiles, sub.m2.Bar.bar));`);
> }
>
> sub/m2.d
> ----------------------
>
> module sub.m2;
>
> class Bar
> {
> 	int bar;
> }
>
> sub/package.d
> ----------------------
>
> module sub;
>
> public import sub.m2;
>
>
> I use DMD master (as I already need the static foreach) on windows 10.
> DMD32 D Compiler v2.075.0-rc1-master-af3eacf
>
>>> [...]
>
> Kind regards
> André

Issue created: https://issues.dlang.org/show_bug.cgi?id=17683

Is there a known workaround?

Kind regards
André