Why does this compile (method in class without return type)
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed May 3 03:18:41 PDT 2017
On Wednesday, 3 May 2017 at 07:34:03 UTC, Daniel Kozák wrote:
> 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
> }
> }
How is it a template in the original example?
final print() { writeln(this); } // no return type
does not have the extra set of parens required to turn it into a
template. It _does_ use inference, just like
static a = 42;
uses inference and
final auto print() { writeln(this); }
uses inference, but it shouldn't be a template any more than
static a = 42;
is a template.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list