digitalmars.D.learn - Why does this compile (method in class without return type)
- nkm1 (32/32) May 02 2017 Consider:
- Jacob Carlborg (10/42) May 03 2017 It might be by accident but I think the compiler is inferring the return...
- Jonathan M Davis (15/22) May 03 2017 Yes, the compiler is inferring the return type. However, it
- Jonathan M Davis (2/3) May 03 2017 https://issues.dlang.org/show_bug.cgi?id=17366
- Adam D. Ruppe (5/8) May 03 2017 No accident there, the spec says any storage class will do:
- Jacob Carlborg (4/8) May 08 2017 I see.
- nkm1 (3/4) May 03 2017 Jacob and Jonathan - thank you, this is clear to me now.
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (31/74) May 03 2017 V Wed, 03 May 2017 06:54:15 +0000
- nkm1 (3/4) May 03 2017 What :)
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (4/11) May 03 2017 V Wed, 03 May 2017 09:21:47 +0000
- Daniel =?UTF-8?B?S296w6Fr?= via Digitalmars-d-learn (4/11) May 03 2017 V Wed, 03 May 2017 09:21:47 +0000
- Jonathan M Davis (4/10) May 03 2017 If it were a template (and it's not), then final would not be
- Jonathan M Davis (12/70) May 03 2017 How is it a template in the original example?
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 02 2017
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
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
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
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:No accident there, the spec says any storage class will do: http://dlang.org/spec/function.html#auto-functions "An auto function is declared without a return type. If it does not already have a storage class, use the auto storage class. "
May 03 2017
On 2017-05-03 14:50, Adam D. Ruppe wrote:No accident there, the spec says any storage class will do: http://dlang.org/spec/function.html#auto-functions "An auto function is declared without a return type. If it does not already have a storage class, use the auto storage class. "I see. -- /Jacob Carlborg
May 08 2017
On Wednesday, 3 May 2017 at 06:54:15 UTC, nkm1 wrote:final method type inference stuffJacob and Jonathan - thank you, this is clear to me now. Hopefully it will get fixed at some point.
May 03 2017
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
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
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:final is not important hereprint in A is template:What :) How does it interact with 'final'?
May 03 2017
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:hmm obviously it is problem only with finalprint in A is template:What :) How does it interact with 'final'?
May 03 2017
On Wednesday, 3 May 2017 at 09:21:47 UTC, nkm1 wrote:On Wednesday, 3 May 2017 at 07:34:03 UTC, Daniel Kozák wrote:If it were a template (and it's not), then final would not be applicable, because templated functions cannot be virtual. - Jonathan M Davisprint in A is template:What :) How does it interact with 'final'?
May 03 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 puremagic.com> napsáno: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 DavisConsider: 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 } }
May 03 2017