compile time method check

Bill Baxter wbaxter at gmail.com
Fri Dec 26 13:57:24 PST 2008


On Sat, Dec 27, 2008 at 6:30 AM, Simen Kjaeraas <simen.kjaras at gmail.com> wrote:
> On Fri, 26 Dec 2008 20:15:30 +0100, Weed <resume755 at mail.ru> wrote:
>
>> Can I at compile time check whether there is a facility method
>> (toString(), for example)?
>>
>> Today I wrote:
>>
>>                static if ( __traits(isArithmetic, Element) ) {
>>                    ret ~= toString(this[i,j]) ~ "\t";
>>                } else {
>>                    ret ~= this[i,j].toString ~ "\t";
>>                }
>>
>> It works but it is wrong
>
> I believe this works:
>
> static if (is(typeof(this[i,j].toString))) {
>        ret ~= this[i,j].toString;
> } else {
>        ret ~= toString(this[i,j]);
> }
>

Additionally, in D2 I think you can do this:   __traits(compiles,
this[i,j].toString)

--bb


More information about the Digitalmars-d-learn mailing list