get parameters of a function
Alex via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Apr 28 23:18:34 PDT 2017
On Saturday, 29 April 2017 at 05:58:35 UTC, Stanislav Blinov
wrote:
> On Friday, 28 April 2017 at 20:43:50 UTC, Alex wrote:
>
> void main()
> {
> foreach(o1; __traits(getOverloads, S1, "opCall"))
> {
> alias P1 = Parameters!o1;
> foreach(o2; __traits(getOverloads, S2, "opCall"))
> {
> alias P2 = Parameters!o2;
> static if (is(P1 == P2))
> {
> pragma(msg, "Matching params: "~P1.stringof);
> }
> }
> }
> }
The problem is another one: say I have something like this:
import std.traits;
struct A(alias T) if(isCallable!T)
{
auto opCall(U...)(U args) if(is(Parameters!T == U))
//if(__traits(compiles, T(args)))
{
return T(args);
}
}
void main()
{
S s;
A!s c;
assert(c(4) == 42);
}
struct S
{
auto opCall(){ return 42; }
auto opCall(int i){return 42; }
}
This doesn't work because of Parameters trait and the both
opCalls defined.
However, I see the point...
If I take the compiles-trait, it works as expected:
In this case the constrained is fulfilled, and with the second
opCall commented out - it isn't.
Thanks :)
More information about the Digitalmars-d-learn
mailing list