digitalmars.D.learn - compile time method check
- Weed (9/9) Dec 26 2008 Can I at compile time check whether there is a facility method
- Simen Kjaeraas (9/18) Dec 26 2008 I believe this works:
- Bill Baxter (4/23) Dec 26 2008 Additionally, in D2 I think you can do this: __traits(compiles,
- Bill Baxter (19/19) Dec 27 2008 MjAwOC8xMi8yNyBXZWVkIDxyZXN1bWU3NTVAbWFpbC5ydT46Cj4gU2ltZW4gS2phZXJhYXMg...
- Weed (3/38) Dec 27 2008 There was confusion between this.toString () and std.stdio.toString ()
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
Dec 26 2008
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 wrongI believe this works: static if (is(typeof(this[i,j].toString))) { ret ~= this[i,j].toString; } else { ret ~= toString(this[i,j]); } -- Simen
Dec 26 2008
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:Additionally, in D2 I think you can do this: __traits(compiles, this[i,j].toString) --bbCan 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 wrongI believe this works: static if (is(typeof(this[i,j].toString))) { ret ~= this[i,j].toString; } else { ret ~= toString(this[i,j]); }
Dec 26 2008
MjAwOC8xMi8yNyBXZWVkIDxyZXN1bWU3NTVAbWFpbC5ydT46Cj4gU2ltZW4gS2phZXJhYXMg0Mnb xdQ6Cj4+IE9uIEZyaSwgMjYgR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BtZXRob2QuICBTbyBpdCBzaG91bGQgYmUKcGlja2luZyB0aGUg ZWxzZXt9IGNhc2UuCgpJZiB5b3UgdHJ5IGl0IHdpdGggc29tZXRoaW5nIHdoZXJlIHRoaXNbaSxq XSByZXR1cm5zIGFuIE9iamVjdCBvcgpzdHJ1Y3Qgd2l0aCBhIHRvU3RyaW5nLCB0aGVuIGl0IHNo b3VsZCBwaWNrIHRoZSAnaWYnIGJyYW5jaC4KCi0tYmIK
Dec 27 2008
Bill Baxter пишет:2008/12/27 Weed <resume755 mail.ru>:There was confusion between this.toString () and std.stdio.toString () Fixed, thanks!Simen Kjaeraas пишет: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.On Fri, 26 Dec 2008 20:15:30 +0100, Weed <resume755 mail.ru> wrote:No, this cause error: toString () does not match parameter types (float) ( condition is always resolved to else {...} )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 wrongI believe this works: static if (is(typeof(this[i,j].toString))) { ret ~= this[i,j].toString; } else { ret ~= toString(this[i,j]); }
Dec 27 2008