Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
December 26, 2008 compile time method check | ||||
---|---|---|---|---|
| ||||
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 |
December 26, 2008 Re: compile time method check | ||||
---|---|---|---|---|
| ||||
Posted in reply to Weed | On Fri, 26 Dec 2008 20:15:30 +0100, Weed <resume755@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]); } -- Simen |
December 26, 2008 Re: compile time method check | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simen Kjaeraas | On Sat, Dec 27, 2008 at 6:30 AM, Simen Kjaeraas <simen.kjaras@gmail.com> wrote:
> On Fri, 26 Dec 2008 20:15:30 +0100, Weed <resume755@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
|
December 27, 2008 Re: compile time method check | ||||
---|---|---|---|---|
| ||||
2008/12/27 Weed <resume755@mail.ru>:
> Simen Kjaeraas пишет:
>> On Fri, 26 Dec 2008 20:15:30 +0100, Weed <resume755@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]);
>> }
>>
>
> 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
|
December 27, 2008 Re: compile time method check | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bill Baxter | Bill Baxter пишет:
> 2008/12/27 Weed <resume755@mail.ru>:
>> Simen Kjaeraas пишет:
>>> On Fri, 26 Dec 2008 20:15:30 +0100, Weed <resume755@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]);
>>> }
>>>
>> 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.
There was confusion between this.toString () and std.stdio.toString ()
Fixed, thanks!
|
Copyright © 1999-2021 by the D Language Foundation