Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
August 15, 2005 How to convert a template parameter into a string? | ||||
---|---|---|---|---|
| ||||
Suppose I have a class template like so: # class Foo(A, B) { # A a; B b; # void dump() { # how to print a as a string? # how to print b as a string? # } # } 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: # template { # char[] toString(X x) { # <insert magic here> # } # } -Chuck |
August 15, 2005 Re: How to convert a template parameter into a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chuck.Esterbrook /at/ gmail /dot/ com | Chuck.Esterbrook /at/ gmail /dot/ com wrote: > Suppose I have a class template like so: > > # class Foo(A, B) { > # A a; B b; > # void dump() { > # how to print a as a string? > # how to print b as a string? > # } > # } > > 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: > > # template { > # char[] toString(X x) { > # <insert magic here> > # } > # } > > -Chuck > > -- Victor (aka nail) Nakoryakov nail-mail<at>mail<dot>ru Krasnoznamensk, Moscow, Russia |
August 15, 2005 Re: How to convert a template parameter into a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Victor Nakoryakov | "Victor Nakoryakov" <nail-mail@mail.ru> wrote in message news:ddpt88$nts$1@digitaldaemon.com... > Chuck.Esterbrook /at/ gmail /dot/ com wrote: >> Suppose I have a class template like so: >> >> # class Foo(A, B) { >> # A a; B b; >> # void dump() { >> # how to print a as a string? >> # how to print b as a string? >> # } >> # } >> >> 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(); I couldn't get this to work. Plus I'd actually consider it a compiler bug if it did work. > 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: >> >> # template { >> # char[] toString(X x) { >> # <insert magic here> >> # } >> # } 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. |
August 15, 2005 Re: How to convert a template parameter into a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Ben Hinkle wrote: > "Victor Nakoryakov" <nail-mail@mail.ru> wrote in message news:ddpt88$nts$1@digitaldaemon.com... > >>Chuck.Esterbrook /at/ gmail /dot/ com wrote: >> >>>Suppose I have a class template like so: >>> >>># class Foo(A, B) { >>># A a; B b; >>># void dump() { >>># how to print a as a string? >>># how to print b as a string? >>># } >>># } >>> >>>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(); > > > I couldn't get this to work. Plus I'd actually consider it a compiler bug if it did work. > 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, Russia |
August 15, 2005 Re: How to convert a template parameter into a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Victor Nakoryakov | 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: # char[] toString(Object obj) { # return obj.toString; # } It seems to me that templates could then simply say: # toString(x); and they would work for both primitives and objects. Right? -Chuck |
August 16, 2005 Re: How to convert a template parameter into a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chuck.Esterbrook /at/ gmail /dot/ com | Chuck.Esterbrook /at/ gmail /dot/ com wrote:
> Suppose I have a class template like so:
>
> # class Foo(A, B) {
> # A a; B b;
> # void dump() {
> # how to print a as a string?
> # how to print b as a string?
> # }
> # }
>
> 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:
# private import std.stdio ;
# private import std.string ;
#
# class Foo (A, B) {
# A a;
# B b;
#
# void dump() {
# writefln("(%s, %s)", a, b);
# }
#
# override char[] toString() {
# return format("[%s (%s, %s)]",
# this.classinfo.name ,
# a.classinfo.name ,
# b.classinfo.name
# );
# }
# }
-- Chris Sauls
|
August 16, 2005 Re: How to convert a template parameter into a string? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chuck.Esterbrook /at/ gmail /dot/ com | Chuck.Esterbrook /at/ gmail /dot/ com wrote: > > It seems to me that templates could then simply say: > # toString(x); > > 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 |
Copyright © 1999-2021 by the D Language Foundation