digitalmars.D.learn - Get the class name without casting the type
- Alexander Zhirov (25/25) Nov 15 2022 Is there any way to get the name of class B?
- Hipreme (3/28) Nov 15 2022 You can do it as `val.classinfo.name`
- Alexander Zhirov (8/9) Nov 15 2022 Yes, I have already done so, but the result is the same, actually
- Imperatorn (37/39) Nov 15 2022 As shown you can use Object for this.
- Alexander Zhirov (4/6) Nov 15 2022 My knowledge of D is still modest, most likely, I just didn't
- bauss (28/53) Nov 15 2022 If you cast to Object and use classinfo.name then you get the
- Alexander Zhirov (2/4) Nov 15 2022 Thanks! 😌
Is there any way to get the name of class B? ```d interface A { string text(); } class B : A { override string text() { return ": It's ok!"; } } void main() { A[] a = cast(A[]) new B[3]; B b = new B(); fill(a, b); foreach (val ; a) { writeln(typeof(val).stringof, val.text()); } } ``` Output: ```sh A: It's ok! A: It's ok! A: It's ok! ```
Nov 15 2022
On Tuesday, 15 November 2022 at 11:42:59 UTC, Alexander Zhirov wrote:Is there any way to get the name of class B? ```d interface A { string text(); } class B : A { override string text() { return ": It's ok!"; } } void main() { A[] a = cast(A[]) new B[3]; B b = new B(); fill(a, b); foreach (val ; a) { writeln(typeof(val).stringof, val.text()); } } ``` Output: ```sh A: It's ok! A: It's ok! A: It's ok! ```You can do it as `val.classinfo.name`
Nov 15 2022
On Tuesday, 15 November 2022 at 12:25:22 UTC, Hipreme wrote:You can do it as `val.classinfo.name`Yes, I have already done so, but the result is the same, actually :) ```d app.A: It's ok! app.A: It's ok! app.A: It's ok! ```
Nov 15 2022
On Tuesday, 15 November 2022 at 12:25:22 UTC, Hipreme wrote:On Tuesday, 15 November 2022 at 11:42:59 UTC, Alexander Zhirov wrote:As shown you can use Object for this. Side-note, you don't override interface members, you implement them. ```d interface A { string text(); } class B : A { string text() { return ": To B or not to B!"; } } class C : A { string text() { return ": Oh I C!"; } } void main() { Object[] a = new B[3]; B b = new B(); C c = new C(); fill(a, b); //Just to show a[0] = c; foreach (val; a) { writeln(typeof(val).stringof, val.text()); } } ```
Nov 15 2022
On Tuesday, 15 November 2022 at 14:26:22 UTC, Imperatorn wrote:Side-note, you don't override interface members, you implement them.My knowledge of D is still modest, most likely, I just didn't know that override with interfaces can not be used. Thanks for the hint!
Nov 15 2022
On Tuesday, 15 November 2022 at 11:42:59 UTC, Alexander Zhirov wrote:Is there any way to get the name of class B? ```d interface A { string text(); } class B : A { override string text() { return ": It's ok!"; } } void main() { A[] a = cast(A[]) new B[3]; B b = new B(); fill(a, b); foreach (val ; a) { writeln(typeof(val).stringof, val.text()); } } ``` Output: ```sh A: It's ok! A: It's ok! A: It's ok! ```If you cast to Object and use classinfo.name then you get the expected result of B. ```d interface A { string text(); } class B : A { override string text() { return ": It's ok!"; } } void main() { A[] a = cast(A[]) new B[3]; B b = new B(); fill(a, b); foreach (val ; a) { writeln((cast(Object)val).classinfo.name, val.text()); // Here is the magic. } ``` Output: ``` onlineapp.B: It's ok! onlineapp.B: It's ok! onlineapp.B: It's ok! ```
Nov 15 2022
On Tuesday, 15 November 2022 at 14:09:01 UTC, bauss wrote:If you cast to Object and use classinfo.name then you get the expected result of B.Thanks! 😌
Nov 15 2022