www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Universel toString

reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
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
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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 Xu
to!(char[]) should call toString. to!(T) should support all atomic types, strings, structs and classes. -- Daniel
Mar 20 2009
next sibling parent reply grauzone <none example.net> writes:
Daniel Keep wrote:
 
 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 Xu
to!(char[]) should call toString. to!(T) should support all atomic types, strings, structs and classes.
So to!(char[])(x) almost like format("%s", x)?
   -- Daniel
Mar 20 2009
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
grauzone wrote:
 Daniel Keep wrote:
 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 Xu
to!(char[]) should call toString. to!(T) should support all atomic types, strings, structs and classes.
So to!(char[])(x) almost like format("%s", x)?
   -- Daniel
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 -- Daniel
Mar 20 2009
prev sibling parent Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
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
prev sibling parent Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
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