Thread overview
Universel toString
Mar 20, 2009
Qian Xu
Mar 20, 2009
Daniel Keep
Mar 20, 2009
grauzone
Mar 20, 2009
Daniel Keep
Mar 20, 2009
Qian Xu
Mar 20, 2009
Qian Xu
March 20, 2009
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
March 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 Xu

to!(char[]) should call toString.  to!(T) should support all atomic
types, strings, structs and classes.

  -- Daniel
March 20, 2009
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
March 20, 2009

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
March 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.

March 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.