www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get the class name without casting the type

reply Alexander Zhirov <azhirov1991 gmail.com> writes:
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
next sibling parent reply Hipreme <msnmancini hotmail.com> writes:
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
next sibling parent Alexander Zhirov <azhirov1991 gmail.com> writes:
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
prev sibling parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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
parent Alexander Zhirov <azhirov1991 gmail.com> writes:
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
prev sibling parent reply bauss <jacobbauss gmail.com> writes:
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
parent Alexander Zhirov <azhirov1991 gmail.com> writes:
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