digitalmars.D - How to convert a template parameter into a string?
- Chuck.Esterbrook /at/ gmail /dot/ com (17/17) Aug 15 2005 Suppose I have a class template like so:
- Victor Nakoryakov (19/43) Aug 15 2005 You can use .toString() even for primitive types. Just define function:
- Ben Hinkle (7/43) Aug 15 2005 I couldn't get this to work. Plus I'd actually consider it a compiler bu...
- Victor Nakoryakov (20/55) Aug 15 2005 Hmm... Really, it does not compile :) I'm sorry for unchecked sample. I
- Chuck.Esterbrook /at/ gmail /dot/ com (11/22) Aug 15 2005 It looks like the only toString() overload missing in std.string is one ...
- Victor Nakoryakov (6/12) Aug 15 2005 As one of variants.
- Chris Sauls (22/36) Aug 15 2005 Can std.string.format() be used for this? Something like:
Suppose I have a class template like so: I thought about using .toString() but that does not work if A and B are primitive types like int. Any suggestions? Is there already a nifty library around along the lines of: -Chuck
Aug 15 2005
Chuck.Esterbrook /at/ gmail /dot/ com wrote:Suppose I have a class template like so: I thought about using .toString() but that does not work if A and B are primitive types like int.You can use .toString() even for primitive types. Just define function: char[] toString(int x) { ... } after that you can write int myint = 65; char[] str = myint.toString(); BTW: toSting()'s of basic types are already implemented in std.string. So you should do following: import std.string; alias toString std.string.toString(); <your magic here> to get expected result.Any suggestions? Is there already a nifty library around along the lines of: -Chuck-- Victor (aka nail) Nakoryakov nail-mail<at>mail<dot>ru Krasnoznamensk, Moscow, Russia
Aug 15 2005
"Victor Nakoryakov" <nail-mail mail.ru> wrote in message news:ddpt88$nts$1 digitaldaemon.com...Chuck.Esterbrook /at/ gmail /dot/ com wrote:I couldn't get this to work. Plus I'd actually consider it a compiler bug if it did work.Suppose I have a class template like so: I thought about using .toString() but that does not work if A and B are primitive types like int.You can use .toString() even for primitive types. Just define function: char[] toString(int x) { ... } after that you can write int myint = 65; char[] str = myint.toString();BTW: toSting()'s of basic types are already implemented in std.string. So you should do following: import std.string; alias toString std.string.toString(); <your magic here> to get expected result.There isn't any template that will work for arbitrary types. For classes Object.toString is the most generic hook. For a struct I'd define a toString method. Otherwise you'll have to use overloading like in std.string.Any suggestions? Is there already a nifty library around along the lines of:
Aug 15 2005
Ben Hinkle wrote:"Victor Nakoryakov" <nail-mail mail.ru> wrote in message news:ddpt88$nts$1 digitaldaemon.com...Hmm... Really, it does not compile :) I'm sorry for unchecked sample. I thought I used such constructions many many times but in practice this was functions for char[]. Plus I'm consider this would _not_ a bug if compiler would accept this. In this case the one thing that came up into my mind is to use: static if (is(A == int) || is(A == uint) || [basic types]) { writefln(std.string.toString(a)); } else { writefln(a.toString); } It was just draft. In practice code can be simplified to look more esthetically. -- Victor (aka nail) Nakoryakov nail-mail<at>mail<dot>ru Krasnoznamensk, Moscow, RussiaChuck.Esterbrook /at/ gmail /dot/ com wrote:I couldn't get this to work. Plus I'd actually consider it a compiler bug if it did work.Suppose I have a class template like so: I thought about using .toString() but that does not work if A and B are primitive types like int.You can use .toString() even for primitive types. Just define function: char[] toString(int x) { ... } after that you can write int myint = 65; char[] str = myint.toString();
Aug 15 2005
In article <ddq7t3$10vk$1 digitaldaemon.com>, Victor Nakoryakov says... [snip]In this case the one thing that came up into my mind is to use: static if (is(A == int) || is(A == uint) || [basic types]) { writefln(std.string.toString(a)); } else { writefln(a.toString); } It was just draft. In practice code can be simplified to look more esthetically.It looks like the only toString() overload missing in std.string is one for object. So if we added: It seems to me that templates could then simply say: and they would work for both primitives and objects. Right? -Chuck
Aug 15 2005
Chuck.Esterbrook /at/ gmail /dot/ com wrote:It seems to me that templates could then simply say: and they would work for both primitives and objects. Right?As one of variants. -- Victor (aka nail) Nakoryakov nail-mail<at>mail<dot>ru Krasnoznamensk, Moscow, Russia
Aug 15 2005
Chuck.Esterbrook /at/ gmail /dot/ com wrote:Suppose I have a class template like so: I thought about using .toString() but that does not work if A and B are primitive types like int. Any suggestions? Is there already a nifty library around along the lines of:Can std.string.format() be used for this? Something like: -- Chris Sauls
Aug 15 2005