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

nkm1 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 2 23:54:15 PDT 2017


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?


More information about the Digitalmars-d-learn mailing list