Checking for Callabilty of either f(x) or x.f

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Aug 1 10:16:17 PDT 2014


On Fri, Aug 01, 2014 at 02:20:53PM +0000, "Nordlöw" via Digitalmars-d-learn wrote:
[...]
> What is the preffered (fast) way to check at compile-time if an
> instance x of a type T can be used *either* as
> 
>     f(x)
> 
> or
> 
>     x.f?

	if (is(typeof(f(x))) || is(typeof(x.f)))

Basically, is(X) checks if X has a valid type (which include void if f
doesn't return anything), and typeof(Y) returns the type of Y if it
exists, otherwise it is an error and has no type. So if f(x) doesn't
compile, then typeof(f(x)) has no type, and so is(typeof(f(x))) will be
false. Ditto for x.f.


T

-- 
All men are mortal. Socrates is mortal. Therefore all men are Socrates.


More information about the Digitalmars-d-learn mailing list