2008/12/27 Weed <[email protected]>:
> Simen Kjaeraas пишет:
>> On Fri, 26 Dec 2008 20:15:30 +0100, Weed <[email protected]> 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]);
>> }
>>
>
> No, this cause error:
>
> toString () does not match parameter types (float)
>
> ( condition is always resolved to else {...} )
That's because float's don't have a toString method. So it should be
picking the else{} case.
If you try it with something where this[i,j] returns an Object or
struct with a toString, then it should pick the 'if' branch.
--bb