Testing template parameter has given API

Paul Backus snarwin at gmail.com
Sat May 16 02:02:47 UTC 2020


On Saturday, 16 May 2020 at 01:11:54 UTC, NaN wrote:
> Howdy, any idea what Im doing wrong? Given a type T i want to 
> test the presence of certain methods. I looked in phobos to see 
> how isInputRange was defined but it didnt really help as all 
> the methods have no parameters and it used some weird lamda 
> stuff i think.
>
>
> template isPathType(T)
> {
>     alias FloatType = ReturnType!(T.x);
>
>     enum isPathType = (isFloatOrDouble!(FloatType)
>         && (is(typeof(&T.x) == FloatType delegate(size_t idx)))
>         && (is(typeof(&T.y) == FloatType delegate(size_t idx)))
>         && (is(typeof(&T.cmd) == PathCmd delegate(size_t idx)))
>         && (is(typeof(&T.points) == Point!FloatType 
> delegate(size_t idx)))
>         && (is(typeof(&T.length) == size_t delegate())));
> }

Rather than trying to inspect the function itself, it's easier to 
check that the result of *calling* the function is what you 
expect it to be. So, for example:

     is(typeof(T.x(size_t.init)) == FloatType)

The above checks to see whether, when you call T.x with a size_t 
argument, you get a FloatType back. If you write your checks this 
way, it doesn't matter whether T.x is a function, a delegate, or 
even a template.


More information about the Digitalmars-d-learn mailing list