www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - compile time method check

reply Weed <resume755 mail.ru> writes:
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
parent reply "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
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
Dec 26 2008
next sibling parent "Bill Baxter" <wbaxter gmail.com> writes:
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
Dec 26 2008
prev sibling parent reply "Bill Baxter" <wbaxter gmail.com> writes:
MjAwOC8xMi8yNyBXZWVkIDxyZXN1bWU3NTVAbWFpbC5ydT46Cj4gU2ltZW4gS2phZXJhYXMg0Mnb
xdQ6Cj4+IE9uIEZyaSwgMjYgRGVjIDIwMDggMjA6MTU6MzAgKzAxMDAsIFdlZWQgPHJlc3VtZTc1
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B0aGUg
ZWxzZXt9IGNhc2UuCgpJZiB5b3UgdHJ5IGl0IHdpdGggc29tZXRoaW5nIHdoZXJlIHRoaXNbaSxq
XSByZXR1cm5zIGFuIE9iamVjdCBvcgpzdHJ1Y3Qgd2l0aCBhIHRvU3RyaW5nLCB0aGVuIGl0IHNo
b3VsZCBwaWNrIHRoZSAnaWYnIGJyYW5jaC4KCi0tYmIK
Dec 27 2008
parent Weed <resume755 mail.ru> writes:
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

static if (is(typeof(this[i,j].toString))) { ret ~= this[i,j].toString; } else { ret ~= toString(this[i,j]); }

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!
Dec 27 2008