Why does this compile (method in class without return type)

Daniel Kozák via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed May 3 00:34:03 PDT 2017


V Wed, 03 May 2017 06:54:15 +0000
nkm1 via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
napsáno:

> Consider:
> 
> import std.stdio;
> 
> class A
> {
>      final print() { writeln(this); } // no return type
> }
> 
> class B : A
> {
>      final void print() { writeln(this); }
> }
> 
> void main()
> {
>      auto b = new B;
>      b.print();
> 
>      A a1 = b;
>      a1.print();
> 
>      A a2 = new A;
>      a2.print();
> }
> 
> That compiles:
> 
> $ dmd -de -w -g ./main.d
> $ main
> main.B
> main.B
> main.A
> 
> with dmd 2.074 on linux:
> 
> $ dmd --version
> DMD64 D Compiler v2.074.0
> Copyright (c) 1999-2017 by Digital Mars written by Walter Bright
> 
> Is that a bug? (in the compiler). I'm learning D, and I'm half 
> way through Andrei's book; I also read the documentation (on D's 
> website) and I think that shouldn't compile?

print in A is template:

import std.stdio;

class A
{
    template print() {
        void print()
        { 
            writeln("A version");
        } // no return type
    }
}

class B : A
{
     final void print() { writeln("B version"); }
}

void main()
{
     auto b = new B;
     b.print();

     A a1 = b;
     a1.print();

     A a2 = new A;
     a2.print();
}

// output:
B version
A version
A version




More information about the Digitalmars-d-learn mailing list