http://d.puremagic.com/issues/show_bug.cgi?id=5354


Max Samukha <samu...@voliacable.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |samu...@voliacable.com


--- Comment #15 from Max Samukha <samu...@voliacable.com> 2011-01-24 05:23:19 
PST ---
(In reply to comment #14)

> 
> bool hasCustomToStringHelper(T)() {
>     foreach(type; TypeTuple!(T,TransitiveBaseTypeTuple!T) ) {
>         if(is(Unqual!type == Object))
>             return false;
>         foreach(member;__traits(derivedMembers,type)) {
>             if(member == "toString")
>                 return true;
>         }
>     }
>     return false;
> }
> template hasCustomToString(T) {
>     static if( __traits(hasMember,T,"toString") ) {
>         static if( is(T==class) ) {
>             enum hasCustomToString = hasCustomToStringHelper!T;
>         } else { // structs, etc
>             enum hasCustomToString = true;
>         }
>     } else {
>         enum hasCustomToString = false;
>     }
> }

This wouldn't work because the dynamic type of the object being formatted can
differ from the static type the template was instantiated with. Even if the
compile-time check won't detect the overriden toString, we would still need to
perform the check at run-time. The easiest way is to compare the address of the
passed-in object's toString to that of Object.toString. Unfortunately that
won't work for objects coming from DLLs since they have distinct
Object.toString functions. So the correct way is to do proper lookup by name
via run-time reflection.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------

Reply via email to