digitalmars.D - Changeing return type of struct.toString()
- Benjamin Thaut (10/10) Feb 12 2012 I'm currently trying to use D without a gc and didn't have major
-
Stewart Gordon
(6/11)
Feb 12 2012
- Timon Gehr (3/18) Feb 12 2012 I suppose he wants to use a custom string type with deterministic memory...
- Robert Jacques (2/12) Feb 12 2012 The primary users of toString() methods (struct or otherwise) are in std...
- Don (4/14) Feb 12 2012 I don't know why struct toString() still exists. It's a legacy from the
- Benjamin Thaut (10/26) Feb 12 2012 Well TypeInfo_struct declarers a delegate for the xtostring member which...
-
Stewart Gordon
(6/7)
Feb 13 2012
- Benjamin Thaut (23/30) Feb 13 2012 import std.stdio;
- Steven Schveighoffer (10/39) Feb 13 2012 That is a legacy feature, back when writefln was a D variadic function
- Benjamin Thaut (9/52) Feb 13 2012 So you want every format like function to be a template instead of a
- Steven Schveighoffer (12/28) Feb 13 2012 No need to imagine, writefln does not use xtoString or the TypeInfo AFAI...
I'm currently trying to use D without a gc and didn't have major problems so far. A minor issues is that struct.toString() returns a string and this will almost allways leak memory when this is called. The question now is, can I change the return type of struct.toString() somewhere inside druntime or is it hardcoded in the compiler? It was possible without any problems to change the return type of Object.toString() and therefore every class in D because Object is the baseclass for every class. Is there something similar for structs? Kind Regards Benjamin Thaut
Feb 12 2012
On 12/02/2012 18:36, Benjamin Thaut wrote:I'm currently trying to use D without a gc and didn't have major problems so far. A minor issues is that struct.toString() returns a string and this will almost allways leak memory when this is called. The question now is, can I change the return type of struct.toString() somewhere inside druntime or is it hardcoded in the compiler?<snip> If you create a struct, you can declare a method called toString with whatever return type you want. Though it usually isn't a good idea. What, exactly, are you planning to do with this non-string string representation of a struct? Stewart.
Feb 12 2012
On 02/13/2012 01:21 AM, Stewart Gordon wrote:On 12/02/2012 18:36, Benjamin Thaut wrote:I suppose he wants to use a custom string type with deterministic memory management.I'm currently trying to use D without a gc and didn't have major problems so far. A minor issues is that struct.toString() returns a string and this will almost allways leak memory when this is called. The question now is, can I change the return type of struct.toString() somewhere inside druntime or is it hardcoded in the compiler?<snip> If you create a struct, you can declare a method called toString with whatever return type you want. Though it usually isn't a good idea. What, exactly, are you planning to do with this non-string string representation of a struct? Stewart.
Feb 12 2012
On Sun, 12 Feb 2012 12:36:47 -0600, Benjamin Thaut <code benjamin-thaut.de> wrote:I'm currently trying to use D without a gc and didn't have major problems so far. A minor issues is that struct.toString() returns a string and this will almost allways leak memory when this is called. The question now is, can I change the return type of struct.toString() somewhere inside druntime or is it hardcoded in the compiler? It was possible without any problems to change the return type of Object.toString() and therefore every class in D because Object is the baseclass for every class. Is there something similar for structs? Kind Regards Benjamin ThautThe primary users of toString() methods (struct or otherwise) are in std.format and std.conv, IIRC. I'd imagine that you'd be able to catch most uses via a correctly placed static assert.
Feb 12 2012
On 12.02.2012 19:36, Benjamin Thaut wrote:I'm currently trying to use D without a gc and didn't have major problems so far. A minor issues is that struct.toString() returns a string and this will almost allways leak memory when this is called. The question now is, can I change the return type of struct.toString() somewhere inside druntime or is it hardcoded in the compiler? It was possible without any problems to change the return type of Object.toString() and therefore every class in D because Object is the baseclass for every class. Is there something similar for structs? Kind Regards Benjamin ThautI don't know why struct toString() still exists. It's a legacy from the very beginning of D, back when the language didn't even have templates. std.format doesn't even require it, any more.
Feb 12 2012
Am 13.02.2012 03:21, schrieb Don:On 12.02.2012 19:36, Benjamin Thaut wrote:Well TypeInfo_struct declarers a delegate for the xtostring member which returns a char[]. So I'm curiours if the compiler fills that member, and if it does any checking on the toString() method while doing so. Basically I have 2 string types now: 1) string = garantueed to be either in static memory or to outlive every object that has a reference to it 2) rcstring = reference counted string (you have to use the most ugly casts you can think of to actually make reference counting possible with the D typesystem)I'm currently trying to use D without a gc and didn't have major problems so far. A minor issues is that struct.toString() returns a string and this will almost allways leak memory when this is called. The question now is, can I change the return type of struct.toString() somewhere inside druntime or is it hardcoded in the compiler? It was possible without any problems to change the return type of Object.toString() and therefore every class in D because Object is the baseclass for every class. Is there something similar for structs? Kind Regards Benjamin ThautI don't know why struct toString() still exists. It's a legacy from the very beginning of D, back when the language didn't even have templates. std.format doesn't even require it, any more.
Feb 12 2012
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.
Feb 13 2012
Am 13.02.2012 13:26, schrieb Stewart Gordon:On 13/02/2012 02:21, Don wrote: <snip>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 ThautI 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.
Feb 13 2012
On Mon, 13 Feb 2012 08:40:20 -0500, Benjamin Thaut <code benjamin-thaut.de> wrote:Am 13.02.2012 13:26, schrieb Stewart Gordon: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. -SteveOn 13/02/2012 02:21, Don wrote: <snip>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.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.
Feb 13 2012
Am 13.02.2012 15:23, schrieb Steven Schveighoffer:On Mon, 13 Feb 2012 08:40:20 -0500, Benjamin Thaut <code benjamin-thaut.de> wrote: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. -- Kind Regards Benjamin ThautAm 13.02.2012 13:26, schrieb Stewart Gordon: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. -SteveOn 13/02/2012 02:21, Don wrote: <snip>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.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.
Feb 13 2012
On Mon, 13 Feb 2012 09:41:14 -0500, Benjamin Thaut <code benjamin-thaut.de> wrote:Am 13.02.2012 15:23, schrieb Steven Schveighoffer: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 -SteveThat 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. -SteveSo 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.
Feb 13 2012