digitalmars.D.learn - static member and/or property ?
- chmike (28/28) May 19 2016 Sorry for the confusing subject, I couldn't find a concise
- Edwin van Leeuwen (4/8) May 19 2016 Can you use identifier!(typeof(this)) or something along those
- Edwin van Leeuwen (2/10) May 19 2016 Sorry I meant __traits(identifier, typeof(this)).
- ag0aep6g (47/65) May 19 2016 Here's what I could come up with. I wouldn't be surprised if there was a...
- chmike (4/20) May 19 2016 This is what I was looking fer. Thank you very much.
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 ?
May 19 2016
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
May 19 2016
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:Sorry I meant __traits(identifier, typeof(this)).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
May 19 2016
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; } ----
May 19 2016
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.
May 19 2016