On Mon, 13 Feb 2012 08:40:20 -0500, Benjamin Thaut <c...@benjamin-thaut.de> wrote:

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.

That is a legacy feature, back when writefln was a D variadic function (not a template), and all you had was the TypeInfo.

It's essentially a hack that the compiler puts several "special" function pointers into the typeinfo to give structs a sort of runtime interface. I think we can get rid of most, if not all, of those x-functions. But then we need to get rid of all the usages of those. It shouldn't be too difficult at this point, it just needs to be done.

-Steve

Reply via email to