www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - uda pattern foo and foo(val)

reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Sep 22 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 23 September 2020 at 01:45:46 UTC, Steven 
Schveighoffer wrote:
  foo int z; // Error: cannot interpret foo(T)(T val) at compile 
 time
Where do you get that error? Is it from phobos' thing? cuz I copy/pasted your code and it compiled. You can also just use a struct as the uda if your detection function checks for both the type and a value of the type, so it really depends on which search method you using.
Sep 22 2020
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On Wednesday, 23 September 2020 at 01:57:08 UTC, Adam D. Ruppe 
wrote:
 On Wednesday, 23 September 2020 at 01:45:46 UTC, Steven 
 Schveighoffer wrote:
  foo int z; // Error: cannot interpret foo(T)(T val) at 
 compile time
Where do you get that error? Is it from phobos' thing? cuz I copy/pasted your code and it compiled. You can also just use a struct as the uda if your detection function checks for both the type and a value of the type, so it really depends on which search method you using.
I just said pragma(msg, __traits(getAttributes, z)) Well actually the thing I tried is slightly different. As usual I dumbed it down to post here (maybe was a bad idea). A struct won’t work because the foo(val) form needs to hold any type and ifti doesn’t work on constructors. -Steve
Sep 22 2020
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 23 September 2020 at 02:07:04 UTC, Steven 
Schveighoffer wrote:
 I just said pragma(msg, __traits(getAttributes, z))
Ah, ok, this is weird, `pragma(msg, __traits(getAttributes, z)[0])` works just fine! But that might be adequate for you - just loop over the attributes and use them one by one. Which you'd do anyway. I suspect this is overload resolution not happening yet when you getAttributes but then it happens when it is forced evaluated with the index. (prolly one of those order-of-semantic bugs in dmd)
Sep 22 2020
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/22/20 10:20 PM, Adam D. Ruppe wrote:
 On Wednesday, 23 September 2020 at 02:07:04 UTC, Steven Schveighoffer 
 wrote:
 I just said pragma(msg, __traits(getAttributes, z))
Ah, ok, this is weird, `pragma(msg, __traits(getAttributes, z)[0])` works just fine!
Oh, yeah, weird! I see that now. So it will actually work, it's just my method of testing "will this work", which actually isn't how I would use it, triggers some obscure bug, lol.
 But that might be adequate for you - just loop over the attributes and 
 use them one by one. Which you'd do anyway.
Yep, exactly what I would be doing anyway.
 I suspect this is overload resolution not happening yet when you 
 getAttributes but then it happens when it is forced evaluated with the 
 index. (prolly one of those order-of-semantic bugs in dmd)
I thought it had something to do with the optional parentheses. Thanks anyway for helping! -Steve
Sep 22 2020
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 9/22/20 11:31 PM, Steven Schveighoffer wrote:

 I thought it had something to do with the optional parentheses.
I think it does have something to do with this. I think it works if you do attrs[0], because you really get an alias to the function symbol, and that calls it. I still think I can work with this anyway. -Steve
Sep 22 2020