Thread overview
Get the class name without casting the type
Nov 15, 2022
Alexander Zhirov
Nov 15, 2022
Hipreme
Nov 15, 2022
Alexander Zhirov
Nov 15, 2022
Imperatorn
Nov 15, 2022
Alexander Zhirov
Nov 15, 2022
bauss
Nov 15, 2022
Alexander Zhirov
November 15, 2022

Is there any way to get the name of class B?

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:

A: It's ok!
A: It's ok!
A: It's ok!
November 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?

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:

A: It's ok!
A: It's ok!
A: It's ok!

You can do it as val.classinfo.name

November 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 :)

app.A: It's ok!
app.A: It's ok!
app.A: It's ok!
November 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?

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:

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.

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!
November 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.

        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());
            }
        }
November 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!

November 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! 😌