digitalmars.D.learn - writeln an object
- gedaiu (5/5) Apr 18 2013 Hi,
- JN (3/8) Apr 18 2013 You can override the toString() method, like this
- gedaiu (6/16) Apr 18 2013 i've done that but i get this error:
- Andrej Mitrovic (2/7) Apr 18 2013 If it's a struct then don't put "override".
- John Colvin (6/15) Apr 18 2013 Just to provide a bit more info:
- gedaiu (13/31) Apr 18 2013 i'm realy sorry... it's my mistake...
- David (10/21) Apr 18 2013 struct Value {
- John Colvin (6/7) Apr 18 2013 There's a slight nomenclature clash here:
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/6) Apr 18 2013 You are missing some words there. :) Not a struct itself, but instances
- gedaiu (5/12) Apr 19 2013 Ok, i understand now the diference...
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (52/54) Apr 19 2013 The keyword 'override' cannot be used with structs. You have at least
Hi, how i can control what writeln outputs when I pass an object parameter? Thanks, Bogdan
Apr 18 2013
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, BogdanYou can override the toString() method, like this http://dpaste.dzfl.pl/db7dbe28
Apr 18 2013
On Thursday, 18 April 2013 at 17:42:53 UTC, JN wrote:On Thursday, 18 April 2013 at 17:36:10 UTC, gedaiu 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 functionsHi, how i can control what writeln outputs when I pass an object parameter? Thanks, BogdanYou can override the toString() method, like this http://dpaste.dzfl.pl/db7dbe28
Apr 18 2013
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 functionsIf it's a struct then don't put "override".
Apr 18 2013
On Thursday, 18 April 2013 at 18:04:03 UTC, Andrej Mitrovic wrote:On 4/18/13, gedaiu <szabobogdan yahoo.com> wrote: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'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 functionsIf it's a struct then don't put "override".
Apr 18 2013
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: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; } }On 4/18/13, gedaiu <szabobogdan yahoo.com> wrote: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'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 functionsIf it's a struct then don't put "override".
Apr 18 2013
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
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
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
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 couldalso be saidto 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
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