Thread overview |
---|
August 12, 2004 Feature request: writefln to call toString() on objects? | ||||
---|---|---|---|---|
| ||||
Consider the following: # import std.stdio; # # class A # { # char[] toString() {return "This is my class";} # } # # void main() # { # writefln(new A); // Outputs some huge random number # } Wouldn't it be more natural for writefln to call the object's toString member? (Is it even possible?) Nick |
August 12, 2004 Re: Feature request: writefln to call toString() on objects? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick | Nick wrote:
> Consider the following:
>
> # import std.stdio;
> #
> # class A
> # {
> # char[] toString() {return "This is my class";}
> # }
> #
> # void main()
> # {
> # writefln(new A); // Outputs some huge random number
> # }
>
> Wouldn't it be more natural for writefln to call the object's toString member?
> (Is it even possible?)
that's the way it's meant to be working. it's even in the format func, well partially. It doesn't work because typeinfo's are still broken. as soon as they are fixed, your example will work perfectly :]
|
August 12, 2004 Re: Feature request: writefln to call toString() on objects? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick | In article <cfgd62$2flg$1@digitaldaemon.com>, Nick says... > >Consider the following: > ># import std.stdio; ># ># class A ># { ># char[] toString() {return "This is my class";} ># } ># ># void main() ># { ># writefln(new A); // Outputs some huge random number ># } > >Wouldn't it be more natural for writefln to call the object's toString member? >(Is it even possible?) > >Nick > > it's suppose to do that, it's just broken, check the bugs group. Ant http://www.digitalmars.com/d/std_format.html#format-string FormatChar 's' The corresponding argument is formatted in a manner consistent with its type: [...] classes derived from Object The result is the string returned from the class instance's .toString() method. A Precision specifies the maximum number of characters to use in the result. |
Copyright © 1999-2021 by the D Language Foundation