Thread overview
Feature request: writefln to call toString() on objects?
Aug 12, 2004
Nick
Aug 12, 2004
h3r3tic
Aug 12, 2004
Ant
August 12, 2004
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
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
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.