Changeing return type of struct.toString()

Benjamin Thaut code at benjamin-thaut.de
Mon Feb 13 05:40:20 PST 2012


Am 13.02.2012 13:26, schrieb Stewart Gordon:
> On 13/02/2012 02:21, Don wrote:
> <snip>
>> I don't know why struct toString() still exists.
> <snip>
>
> What are you talking about? If you define a struct, it doesn't have any
> methods other than the ones you put into it.
>
> Stewart.

import std.stdio;

struct fun
{
   string toString()
   {
     return "fun";
   }
}

void main(string[] args)
{
   auto ti = cast(TypeInfo_Struct)typeid(fun);
   fun gun;
   writefln("%d",cast(void*)ti.xtoString);
   writefln("%s",ti.xtoString(&gun));
}

If you change the return type of toString() to int for example the 
program will crash because ti.xtoString will be null. Therefore the 
compiler seems to check what type toString() does return and only fills 
the type info if it actually does return a string.

-- 
Kind Regards
Benjamin Thaut


More information about the Digitalmars-d mailing list