Re: static member and/or @property ?
On Thursday, 19 May 2016 at 15:33:21 UTC, ag0aep6g wrote: . . . interface AThing { final string name() { return nameImpl(); } string nameImpl(); } class OneThing : AThing { static string name() { return "OneThing"; } override string nameImpl() { return name(); } } class OtherThing : AThing { static string name() { return "OtherThing"; } override string nameImpl() { return name(); } } This is what I was looking fer. Thank you very much. The fact that I used the class name is just a coincidence for making the example clear.
Re: static member and/or @property ?
On 05/19/2016 05:04 PM, chmike wrote: interface AThing { ??? string name ??? } class OneThing : AThing { ??? string name ??? "OneThing" ??? } class OtherThing : AThing { ??? string name ??? "OtherThing" ??? } void main() { // Accessing name as static information writeln(OneThing.name ??? ); // Accessing name through polymorphism AThing tbl; tbl ~= new OneThing; tbl ~= new OtherThing; tbl ~= new OneThing; foreach(a; tbl) writeln(a.name???); } The only viable solution I found so far is by using distinct member names. In the interface we define name as a property, and in the class we define the static member with another name. Is it possible to avoid the different names ? Here's what I could come up with. I wouldn't be surprised if there was a simpler way to do it. interface AThing { final string name() { return nameImpl(); } string nameImpl(); } class OneThing : AThing { static string name() { return "OneThing"; } override string nameImpl() { return name(); } } class OtherThing : AThing { static string name() { return "OtherThing"; } override string nameImpl() { return name(); } } void main() { import std.stdio; // Accessing name as static information writeln(OneThing.name); // Accessing name through polymorphism AThing[] tbl; tbl ~= new OneThing; tbl ~= new OtherThing; tbl ~= new OneThing; foreach(a; tbl) writeln(a.name); } Can use a mixin template to reduce the boilerplate: interface AThing { final string name() { return nameImpl(); } string nameImpl(); mixin template nameMixin() { static string name() { return typeof(this).stringof; } override string nameImpl() { return name(); } } } class OneThing : AThing { mixin AThing.nameMixin; } class OtherThing : AThing { mixin AThing.nameMixin; }
Re: static member and/or @property ?
On Thursday, 19 May 2016 at 15:12:44 UTC, Edwin van Leeuwen wrote: On Thursday, 19 May 2016 at 15:04:00 UTC, chmike wrote: The only viable solution I found so far is by using distinct member names. In the interface we define name as a property, and in the class we define the static member with another name. Is it possible to avoid the different names ? Can you use identifier!(typeof(this)) or something along those lines. I am not sure how it behaves with inheritance. https://dlang.org/spec/traits.html#identifier Sorry I meant __traits(identifier, typeof(this)).
Re: static member and/or @property ?
On Thursday, 19 May 2016 at 15:04:00 UTC, chmike wrote: The only viable solution I found so far is by using distinct member names. In the interface we define name as a property, and in the class we define the static member with another name. Is it possible to avoid the different names ? Can you use identifier!(typeof(this)) or something along those lines. I am not sure how it behaves with inheritance. https://dlang.org/spec/traits.html#identifier
static member and/or @property ?
Sorry for the confusing subject, I couldn't find a concise formulation of my question. I have a set of classes derived from the same interface so that I can use polymorphism. I would like to store the name of the class as a string so that it can be retrieved as a static member of the class or by using polymorphism. Here is the problem expressed as an exercise where you are invited to replace the ??? with the appropriate text, if it is possible. My blind and naive attempts failed. interface AThing { ??? string name ??? } class OneThing : AThing { ??? string name ??? "OneThing" ??? } class OtherThing : AThing { ??? string name ??? "OtherThing" ??? } void main() { // Accessing name as static information writeln(OneThing.name ??? ); // Accessing name through polymorphism AThing tbl; tbl ~= new OneThing; tbl ~= new OtherThing; tbl ~= new OneThing; foreach(a; tbl) writeln(a.name???); } The only viable solution I found so far is by using distinct member names. In the interface we define name as a property, and in the class we define the static member with another name. Is it possible to avoid the different names ?