uda pattern foo and foo(val)

Steven Schveighoffer schveiguy at gmail.com
Wed Sep 23 01:45:46 UTC 2020


I want to set up a way to use the following patterns:

@foo
@foo(val)

Is there a way I can define foo such that this works?

I tried this:

struct Foo(T)
{
    T val;
}

auto foo(T)(T val) { return Foo!T(val); }

@property foo() { return Foo!int(0); }

So this works:

@foo() int x;
@foo(1) int y;

But this doesn't:

@foo int z; // Error: cannot interpret foo(T)(T val) at compile time

Is there a way I can do this while keeping the name the same for both 
options?

If I have different names, I can get it to work also.

-Steve


More information about the Digitalmars-d-learn mailing list