digitalmars.D.learn - Universel toString
- Qian Xu (37/37) Mar 20 2009 Hi All,
- Daniel Keep (4/17) Mar 20 2009 to!(char[]) should call toString. to!(T) should support all atomic
- grauzone (2/22) Mar 20 2009
- Daniel Keep (8/34) Mar 20 2009 Probably something like
- Qian Xu (5/9) Mar 20 2009 There is one problem: I have to check, whether a pointer is NULL.
- Qian Xu (12/12) Mar 20 2009 The problem has been solved.
Hi All, I want to write an universel toString() method for debugging propose. However I cannot write in one version. The compiler says error all the time. Could someone tell me, how to write an universel one? What I want, is to convert primtive types (int, int*, bool, bool*, etc.) to string using "tango.text.Convert.to()" and convert object to string by calling obj.toString. ----------------------- my current version ------------------------- public char[] toS(int* val) { if (val is null) return "NULL"; return to!(char[])(*val); } public char[] toS(Object val) { if (val is null) return "NULL"; return val.toString; } public char[] toS(char[] val) { if (val is null) return "NULL"; return val; } public char[] toS(int val) { return to!(char[])(val); } public char[] toS(bool val) { return to!(char[])(val); } ----------------------- my current version ------------------------- Best regards --Qian Xu
Mar 20 2009
Qian Xu wrote:Hi All, I want to write an universel toString() method for debugging propose. However I cannot write in one version. The compiler says error all the time. Could someone tell me, how to write an universel one? What I want, is to convert primtive types (int, int*, bool, bool*, etc.) to string using "tango.text.Convert.to()" and convert object to string by calling obj.toString. ... Best regards --Qian Xuto!(char[]) should call toString. to!(T) should support all atomic types, strings, structs and classes. -- Daniel
Mar 20 2009
Daniel Keep wrote:Qian Xu wrote:So to!(char[])(x) almost like format("%s", x)?Hi All, I want to write an universel toString() method for debugging propose. However I cannot write in one version. The compiler says error all the time. Could someone tell me, how to write an universel one? What I want, is to convert primtive types (int, int*, bool, bool*, etc.) to string using "tango.text.Convert.to()" and convert object to string by calling obj.toString. ... Best regards --Qian Xuto!(char[]) should call toString. to!(T) should support all atomic types, strings, structs and classes.-- Daniel
Mar 20 2009
grauzone wrote:Daniel Keep wrote:Probably something like tango.text.convert.Format.Format.convert("{0}",x) for Tango. Actually, that's not entirely true. to!(char[]) won't convert pointers or arrays. Sorry; the brain switched off as soon as I saw "I have to special-case to" :P -- DanielQian Xu wrote:So to!(char[])(x) almost like format("%s", x)?Hi All, I want to write an universel toString() method for debugging propose. However I cannot write in one version. The compiler says error all the time. Could someone tell me, how to write an universel one? What I want, is to convert primtive types (int, int*, bool, bool*, etc.) to string using "tango.text.Convert.to()" and convert object to string by calling obj.toString. ... Best regards --Qian Xuto!(char[]) should call toString. to!(T) should support all atomic types, strings, structs and classes.-- Daniel
Mar 20 2009
Daniel Keep wrote:to!(char[]) should call toString. to!(T) should support all atomic types, strings, structs and classes.There is one problem: I have to check, whether a pointer is NULL. If it is NULL, I should return "NULL", otherwise I should call to!(char[] (*my_pointer_var) I want to save this check.
Mar 20 2009
The problem has been solved. There is a wonderfull function in Tango. -------------------------------------------- import tango.text.convert.Format; class Foo { // attributes go here... public char[] toString() { return Format.convert(".... {} .. {} ..", attr1, attr2, attr3); } } -------------------------------------------- This works perfect with all data types.
Mar 20 2009