Function signature testing with is expression.

ag0aep6g anonymous at example.com
Sun Dec 17 16:19:00 UTC 2017


On Sunday, 17 December 2017 at 14:44:15 UTC, Alexandru Ermicioi 
wrote:
> Suppose:
> --------
> struct F {
>     static void foo(T)(T i, int o) {}
> }
>
> enum bool check(T) = is(F.foo!T == void function(Z, int), Z);
>
> enum correct = check!int;
> --------
>
> Upon running it will return false, though, by logic is 
> expression could deduce that F.foo is conformat to function 
> signature in check test.

Here, `F.foo!T` is the function itself, not its type. You forgot 
`typeof`.

> It is interesting that it will not work with global functions 
> as well:
>
> --------
> void foo(int i, double d) {};
> enum bool check = is(typeof(foo) == void function(int, double));
> --------
>
> It will be as well evaluated to false.

Write `typeof(&foo)` to make it work.

There are two kinds of function types in D:

1) "Proper" function types, e.g. `typeof(foo)` which gets printed 
as "void(int i, double d)", and
2) function pointer types, e.g. `typeof(&foo)` which gets printed 
as "void function(int i, double d)".

As you see, the second kind is the one you're comparing against. 
I don't think you can use the first kind directly in an 
IsExpression. The first kind is really rather useless, as far as 
I know. Argubaly, the language would be nicer without it.


More information about the Digitalmars-d-learn mailing list