[Issue 5354] formatValue: range templates introduce 3 bugs related to class & struct cases

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jan 24 05:25:27 PST 2011


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


Max Samukha <samukha at voliacable.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |samukha at voliacable.com


--- Comment #15 from Max Samukha <samukha at 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: -------


More information about the Digitalmars-d-bugs mailing list