www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Feature request: writefln to call toString() on objects?

reply Nick <Nick_member pathlink.com> writes:
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
Aug 12 2004
next sibling parent h3r3tic <h3r3tic dev.null> writes:
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 :]
Aug 12 2004
prev sibling parent Ant <Ant_member pathlink.com> writes:
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

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.
Aug 12 2004