Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 03, 2017 Why does this compile (method in class without return type) | ||||
---|---|---|---|---|
| ||||
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? |
May 03, 2017 Re: Why does this compile (method in class without return type) | ||||
---|---|---|---|---|
| ||||
Posted in reply to nkm1 | V Wed, 03 May 2017 06:54:15 +0000 nkm1 via Digitalmars-d-learn <digitalmars-d-learn@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 |
May 03, 2017 Re: Why does this compile (method in class without return type) | ||||
---|---|---|---|---|
| ||||
Posted in reply to nkm1 | On 2017-05-03 08:54, nkm1 wrote: > 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? It might be by accident but I think the compiler is inferring the return type. Just as "auto" is not necessary to infer the type of a variable if there's another attribute: auto a = 3; const auto b = 4; // here "auto" is redundant const c = 5; In your case you have "final" as the attribute. -- /Jacob Carlborg |
May 03, 2017 Re: Why does this compile (method in class without return type) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Wednesday, 3 May 2017 at 07:37:31 UTC, Jacob Carlborg wrote:
> It might be by accident but I think the compiler is inferring the return type. Just as "auto" is not necessary to infer the type of a variable if there's another attribute:
>
> auto a = 3;
> const auto b = 4; // here "auto" is redundant
> const c = 5;
>
> In your case you have "final" as the attribute.
Yes, the compiler is inferring the return type. However, it _should_ be inferring it as void, and the override in B should then be illegal. If I stick
pragma(msg, typeof(print));
right after each declaration for print, then it prints
@system void()
for A and
void()
for B, which is odd, since both should be @system. Interestingly, if both have pragmas at the same time, _then_ I get a compilation error:
qd.d(11): Error: function qd.B.print cannot override final function qd.A.print
So, I'd say that there's definitely a bug here.
|
May 03, 2017 Re: Why does this compile (method in class without return type) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | On Wednesday, 3 May 2017 at 08:10:04 UTC, Jonathan M Davis wrote: > So, I'd say that there's definitely a bug here. https://issues.dlang.org/show_bug.cgi?id=17366 |
May 03, 2017 Re: Why does this compile (method in class without return type) | ||||
---|---|---|---|---|
| ||||
Posted in reply to nkm1 | On Wednesday, 3 May 2017 at 06:54:15 UTC, nkm1 wrote:
> final method type inference stuff
Jacob and Jonathan - thank you, this is clear to me now. Hopefully it will get fixed at some point.
|
May 03, 2017 Re: Why does this compile (method in class without return type) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozák | On Wednesday, 3 May 2017 at 07:34:03 UTC, Daniel Kozák wrote:
>
> print in A is template:
>
What :)
How does it interact with 'final'?
|
May 03, 2017 Re: Why does this compile (method in class without return type) | ||||
---|---|---|---|---|
| ||||
Posted in reply to nkm1 | V Wed, 03 May 2017 09:21:47 +0000 nkm1 via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsáno: > On Wednesday, 3 May 2017 at 07:34:03 UTC, Daniel Kozák wrote: > > > > print in A is template: > > > > What :) > How does it interact with 'final'? final is not important here |
May 03, 2017 Re: Why does this compile (method in class without return type) | ||||
---|---|---|---|---|
| ||||
Posted in reply to nkm1 | V Wed, 03 May 2017 09:21:47 +0000 nkm1 via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> napsáno: > On Wednesday, 3 May 2017 at 07:34:03 UTC, Daniel Kozák wrote: > > > > print in A is template: > > > > What :) > How does it interact with 'final'? hmm obviously it is problem only with final |
May 03, 2017 Re: Why does this compile (method in class without return type) | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozák | 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@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
|
Copyright © 1999-2021 by the D Language Foundation