Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 21, 2012 interface final members | ||||
---|---|---|---|---|
| ||||
interface I { final int foo(I other, int a, int b) { return other.foo(a,b) + a*b; } int foo(int a, int b); } class A : I { int foo(int a, int b) { return a*b; } } void main() { A a = new A; a.foo(5,5); a.I.foo(a, 5,5); a.foo(a, 5,5); //line 22 } --------- $ rdmd interface_final_test interface_final_test.d(22): Error: function interface_final_test.A.foo (int a, int b) is not callable using argument types (A,int,int) interface_final_test.d(22): Error: expected 2 arguments, not 3 for non-variadic function type int(int a, int b) --------- Why do I need to write a.I.foo instead of a.foo to call the final method of the interface ? Thank you, Joshua |
February 21, 2012 Re: interface final members | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joshua Reusch | 21.02.2012 14:46, Joshua Reusch пишет:
> interface I {
> final int foo(I other, int a, int b) {
> return other.foo(a,b) + a*b;
> }
> int foo(int a, int b);
> }
>
> class A : I {
> int foo(int a, int b) {
> return a*b;
> }
> }
>
> void main() {
> A a = new A;
>
> a.foo(5,5);
> a.I.foo(a, 5,5);
> a.foo(a, 5,5); //line 22
> }
> ---------
> $ rdmd interface_final_test
> interface_final_test.d(22): Error: function interface_final_test.A.foo (int a, int b) is not callable using argument types (A,int,int)
> interface_final_test.d(22): Error: expected 2 arguments, not 3 for non-variadic function type int(int a, int b)
> ---------
>
>
> Why do I need to write a.I.foo instead of a.foo to call the final method of the interface ?
>
> Thank you, Joshua
>
>
I can't comment on the behaviour, but you may find this workaround useful:
class A : I {
alias I.foo foo;
int foo(int a, int b) {
return a*b;
}
}
|
February 21, 2012 Re: interface final members | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mantis | On 2012-02-21 14:15, Mantis wrote: > 21.02.2012 14:46, Joshua Reusch пишет: >> interface I { >> final int foo(I other, int a, int b) { >> return other.foo(a,b) + a*b; >> } >> int foo(int a, int b); >> } >> >> class A : I { >> int foo(int a, int b) { >> return a*b; >> } >> } >> >> void main() { >> A a = new A; >> >> a.foo(5,5); >> a.I.foo(a, 5,5); >> a.foo(a, 5,5); //line 22 >> } >> --------- >> $ rdmd interface_final_test >> interface_final_test.d(22): Error: function interface_final_test.A.foo >> (int a, int b) is not callable using argument types (A,int,int) >> interface_final_test.d(22): Error: expected 2 arguments, not 3 for >> non-variadic function type int(int a, int b) >> --------- >> >> >> Why do I need to write a.I.foo instead of a.foo to call the final >> method of the interface ? >> >> Thank you, Joshua >> >> > > I can't comment on the behaviour, but you may find this workaround useful: > > class A : I { > alias I.foo foo; > int foo(int a, int b) { > return a*b; > } > } It's because the base class and the subclass use different overload sets, or something like that. -- /Jacob Carlborg |
February 21, 2012 Re: interface final members | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joshua Reusch | On 02/21/2012 04:46 AM, Joshua Reusch wrote:
> interface I {
> final int foo(I other, int a, int b) {
> return other.foo(a,b) + a*b;
> }
> int foo(int a, int b);
> }
>
> class A : I {
> int foo(int a, int b) {
> return a*b;
> }
> }
>
> void main() {
> A a = new A;
>
> a.foo(5,5);
> a.I.foo(a, 5,5);
> a.foo(a, 5,5); //line 22
> }
> ---------
> $ rdmd interface_final_test
> interface_final_test.d(22): Error: function interface_final_test.A.foo
> (int a, int b) is not callable using argument types (A,int,int)
> interface_final_test.d(22): Error: expected 2 arguments, not 3 for
> non-variadic function type int(int a, int b)
> ---------
>
>
> Why do I need to write a.I.foo instead of a.foo to call the final method
> of the interface ?
>
> Thank you, Joshua
>
Are you using 2.058? If so, this may be a bad interaction with the newly-added UFCS feature, which I haven't seen working yet. :)
The reason that I think so is that when the 'I other' is moved to a parameter location other than the first one, it works:
interface I {
final int foo(int a, I other, int b) {// <- second parameter
return other.foo(a,b) + a*b;
}
int foo(int a, int b);
}
class A : I {
int foo(int a, int b) {
return a*b;
}
}
void main() {
A a = new A;
a.foo(5,5);
a.I.foo(5, a, 5);
a.foo(5,5); //line 22
}
I would say this warrants a bug report. The original code should have worked too.
Ali
|
February 21, 2012 Re: interface final members | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On 02/21/2012 09:58 AM, Ali Çehreli wrote:
> The reason that I think so is that when the 'I other' is moved to a
> parameter location other than the first one, it works:
No, it doesn't work. Sorry for the noise.
Ali
|
Copyright © 1999-2021 by the D Language Foundation