Asking types about their traits (D 1.0)

Jarrett Billingsley kb3ctd2 at yahoo.com
Sun Feb 10 18:42:20 PST 2008


"Burton Radons" <burton-radons at shaw.ca> wrote in message 
news:foo388$qsh$1 at digitalmars.com...

>
> But I'd like it if the presence of a method didn't require a superfluous 
> alias, but could be detected automatically with something like "is (T.pow 
> : T delegate (T))". Any ideas?
>
> Maybe I shouldn't be fighting this - the presence of a method even with 
> the correct signature doesn't necessarily imply that it supports the 
> functionality we're requesting. On the other hand, the likelihood of 
> incorrect code being generated seems extremely minute versus the expense 
> of the alias. What do you think?

struct A
{

}

struct B
{
    B pow(B other)
    {
         return B();
    }
}

template HasPow(T)
{
    const HasPow = is(typeof(&T.pow) == T function(T));
}

pragma(msg, HasPow!(A) ? "true" : "false"); // prints false
pragma(msg, HasPow!(B) ? "true" : "false"); // prints true

You were pretty close! 




More information about the Digitalmars-d-learn mailing list