Changeing return type of struct.toString()

Steven Schveighoffer schveiguy at yahoo.com
Mon Feb 13 06:54:12 PST 2012


On Mon, 13 Feb 2012 09:41:14 -0500, Benjamin Thaut  
<code at benjamin-thaut.de> wrote:

> Am 13.02.2012 15:23, schrieb Steven Schveighoffer:
>> 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
>
> So you want every format like function to be a template instead of a  
> variadic function? I actually found it pretty nice that printing structs  
> is possible without templates. I don't want to imagine how big the code  
> bload would be if you actually make format a template instead of a  
> variadic function.

No need to imagine, writefln does not use xtoString or the TypeInfo AFAIK.

But in any case, the template can just be glue code.  All the TypeInfo  
does is store a function pointer to the toString.  If you took that  
delegate from each item, and then passed it into a non-template function,  
the bulk of the format code would not be repeated in the template.

And it would be more robust -- toString wouldn't have to necessarily be  
always the same signature.

All this is not the right way to do formatting anyway, see DIP9  
http://prowiki.org/wiki4d/wiki.cgi?LanguageDevel/DIPs/DIP9

-Steve


More information about the Digitalmars-d mailing list