www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - writeln an object

reply "gedaiu" <szabobogdan yahoo.com> writes:
Hi,

how i can control what writeln outputs when I pass an object 
parameter?

Thanks,
Bogdan
Apr 18 2013
parent reply "JN" <666total wp.pl> writes:
On Thursday, 18 April 2013 at 17:36:10 UTC, gedaiu wrote:
 Hi,

 how i can control what writeln outputs when I pass an object 
 parameter?

 Thanks,
 Bogdan
You can override the toString() method, like this http://dpaste.dzfl.pl/db7dbe28
Apr 18 2013
parent reply "gedaiu" <szabobogdan yahoo.com> writes:
On Thursday, 18 April 2013 at 17:42:53 UTC, JN wrote:
 On Thursday, 18 April 2013 at 17:36:10 UTC, gedaiu wrote:
 Hi,

 how i can control what writeln outputs when I pass an object 
 parameter?

 Thanks,
 Bogdan
You can override the toString() method, like this http://dpaste.dzfl.pl/db7dbe28
i've done that but i get this error: Error: function base.Value.Value.toString cannot override a non-virtual function Error: function base.Value.Value.toString override only applies to class member functions
Apr 18 2013
parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 4/18/13, gedaiu <szabobogdan yahoo.com> wrote:
 i've done that but i get this error:

 Error: function base.Value.Value.toString cannot override a
 non-virtual function
 Error: function base.Value.Value.toString override only applies
 to class member functions
If it's a struct then don't put "override".
Apr 18 2013
parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 18 April 2013 at 18:04:03 UTC, Andrej Mitrovic wrote:
 On 4/18/13, gedaiu <szabobogdan yahoo.com> wrote:
 i've done that but i get this error:

 Error: function base.Value.Value.toString cannot override a
 non-virtual function
 Error: function base.Value.Value.toString override only applies
 to class member functions
If it's a struct then don't put "override".
Just to provide a bit more info: Classes all derive from Object, which defines toString. Hence, you need to override it to define your own. Structs don't have a parent (or any inheritance at all) and hence you don't override anything, you just define the method.
Apr 18 2013
parent reply "gedaiu" <szabobogdan yahoo.com> writes:
On Thursday, 18 April 2013 at 18:25:21 UTC, John Colvin wrote:
 On Thursday, 18 April 2013 at 18:04:03 UTC, Andrej Mitrovic 
 wrote:
 On 4/18/13, gedaiu <szabobogdan yahoo.com> wrote:
 i've done that but i get this error:

 Error: function base.Value.Value.toString cannot override a
 non-virtual function
 Error: function base.Value.Value.toString override only 
 applies
 to class member functions
If it's a struct then don't put "override".
Just to provide a bit more info: Classes all derive from Object, which defines toString. Hence, you need to override it to define your own. Structs don't have a parent (or any inheritance at all) and hence you don't override anything, you just define the method.
i'm realy sorry... it's my mistake... i have a struct not an object. I have someting like this when i get the error: struct Value { string strVal; this(string val) { strVal = val; } override string toString() { return strVal; } }
Apr 18 2013
next sibling parent David <d dav1d.de> writes:
Just drop the override:

 struct Value {
     string strVal;
 
         this(string val) {
         strVal = val;
     }
 
         override string toString() {
         return strVal;
     }
 }
struct Value { string strVal; this(string val) { strVal = val; } string toString() { return strVal; } }
Apr 18 2013
prev sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
On Thursday, 18 April 2013 at 18:46:09 UTC, gedaiu wrote:
 i have a struct not an object.
There's a slight nomenclature clash here: Object is the base class in D. Therefore one could say that an object is an instatiation of Object and therefore a class. However, by a wider definition of the word, a struct could also be said to be an object.
Apr 18 2013
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/18/2013 12:37 PM, John Colvin wrote:

 However, by a wider definition of the word, a struct could also be said
 to be an object.
You are missing some words there. :) Not a struct itself, but instances of it are said to be objects. Ali
Apr 18 2013
parent reply "gedaiu" <szabobogdan yahoo.com> writes:
Ok, i understand now the diference...

my question is how I should solve this problem?

Thanks,
Bogdan

On Thursday, 18 April 2013 at 20:57:23 UTC, Ali Çehreli wrote:
 On 04/18/2013 12:37 PM, John Colvin wrote:

 However, by a wider definition of the word, a struct could
also be said
 to be an object.
You are missing some words there. :) Not a struct itself, but instances of it are said to be objects. Ali
Apr 19 2013
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 04/19/2013 01:35 PM, gedaiu wrote:

 Ok, i understand now the diference...

 my question is how I should solve this problem?
The keyword 'override' cannot be used with structs. You have at least two good options with structs: 1) Define a const toString() member function that returns the string representation of the object, conveniently produced by std.string.format: import std.stdio; import std.string; struct TimeOfDay { int hour; int minute; string toString() const { return format("%02s:%02s", hour, minute); } } void main() { auto t = TimeOfDay(10, 20); writeln(t); } 2) As the previous method is unnecessarily inefficient especially when the members are structs as well, define the toString() overload that takes a sink delegate. std.format.formattedWrite is smart enough to take advantage of it and avoids multiple string instances. Everything gets appended to the same output string: import std.stdio; import std.format; struct TimeOfDay { int hour; int minute; void toString(scope void delegate(const(char)[]) output) const { formattedWrite(output, "%02s:%02s", hour, minute); } } struct Duration { TimeOfDay begin; TimeOfDay end; void toString(scope void delegate(const(char)[]) output) const { formattedWrite(output, "from %s to %s", begin, end); } } void main() { auto d = Duration(TimeOfDay(10, 20), TimeOfDay(11, 22)); writeln(d); } Ali
Apr 19 2013