Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
April 28, 2013 function overrides but is not covariant | ||||
---|---|---|---|---|
| ||||
That surprised me a bit. Is that expected? ---- import std.stdio; struct A { } interface IFoo { void bar(ref const A); } class Foo : IFoo { void bar(ref const A a) { } void bar(const A a) { return this.bar(a); } } ---- prints: Error: function c517.Foo.bar of type void(const(A) a) overrides but is not covariant with c517.IFoo.bar of type void(ref const(A)) |
April 29, 2013 Re: function overrides but is not covariant | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | Nobody knows? |
April 29, 2013 Re: function overrides but is not covariant | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On Sunday, 28 April 2013 at 19:45:41 UTC, Namespace wrote:
> That surprised me a bit. Is that expected?
>
> ----
> import std.stdio;
>
> struct A { }
>
> interface IFoo {
> void bar(ref const A);
> }
>
> class Foo : IFoo {
> void bar(ref const A a) {
>
> }
>
> void bar(const A a) {
> return this.bar(a);
> }
> }
> ----
> prints:
>
> Error: function c517.Foo.bar of type void(const(A) a) overrides but is not covariant with c517.IFoo.bar of type void(ref const(A))
Not surprising to me at all. Why would ref be covariant with non-ref?
|
April 29, 2013 Re: function overrides but is not covariant | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | > Not surprising to me at all. Why would ref be covariant with non-ref?
I do not understand the error fully. Why I cannot overload the method in the class with non-ref?
|
April 29, 2013 Re: function overrides but is not covariant | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On 04/28/2013 09:45 PM, Namespace wrote:
> That surprised me a bit. Is that expected?
>
> ----
> import std.stdio;
>
> struct A { }
>
> interface IFoo {
> void bar(ref const A);
> }
>
> class Foo : IFoo {
> void bar(ref const A a) {
>
> }
>
> void bar(const A a) {
> return this.bar(a);
> }
> }
> ----
> prints:
>
> Error: function c517.Foo.bar of type void(const(A) a) overrides but is
> not covariant with c517.IFoo.bar of type void(ref const(A))
Seems to be a bug.
|
April 29, 2013 Re: function overrides but is not covariant | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On Monday, 29 April 2013 at 09:23:01 UTC, Namespace wrote:
>> Not surprising to me at all. Why would ref be covariant with non-ref?
>
> I do not understand the error fully. Why I cannot overload the method in the class with non-ref?
Sorry, my mistake, it looks like a bug. Dmd thinks that you're trying to override, not overload.
|
April 29, 2013 Re: function overrides but is not covariant | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On 04/28/2013 12:45 PM, Namespace wrote: > That surprised me a bit. Is that expected? > > ---- > import std.stdio; > > struct A { } > > interface IFoo { > void bar(ref const A); > } > > class Foo : IFoo { > void bar(ref const A a) { > > } > > void bar(const A a) { > return this.bar(a); > } > } > ---- > prints: > > Error: function c517.Foo.bar of type void(const(A) a) overrides but is > not covariant with c517.IFoo.bar of type void(ref const(A)) As others have said, one would expect this to compile because it is very common to overload on the same struct type, one taking by-value, the other taking by-reference. The former is mainly to capture rvalue struct objects and the latter is for lvalues. To match what is common in Phobos and TDPL is to define the by-value overload without 'const': void bar(A a) { return this.bar(a); } This example now compiles. Ali |
April 29, 2013 Re: function overrides but is not covariant | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Monday, 29 April 2013 at 11:40:45 UTC, Timon Gehr wrote:
> On 04/28/2013 09:45 PM, Namespace wrote:
>> That surprised me a bit. Is that expected?
>>
>> ----
>> import std.stdio;
>>
>> struct A { }
>>
>> interface IFoo {
>> void bar(ref const A);
>> }
>>
>> class Foo : IFoo {
>> void bar(ref const A a) {
>>
>> }
>>
>> void bar(const A a) {
>> return this.bar(a);
>> }
>> }
>> ----
>> prints:
>>
>> Error: function c517.Foo.bar of type void(const(A) a) overrides but is
>> not covariant with c517.IFoo.bar of type void(ref const(A))
>
> Seems to be a bug.
I filled a new bug report. But the title is broken because of the very nice copy functionality of my tablet.
|
Copyright © 1999-2021 by the D Language Foundation