covariance of 'out' parameters is crucial for polymorphism and function subtyping
Boscop
nospam at example.com
Fri Mar 9 16:59:28 PST 2012
I noticed that D doesn't support covariant 'out' parameters,
which breaks polymorphism that involves such functions, e.g.:
---
// these classes' foo method returns via return value
class A {A foo(){return new A;}}
class B : A {B foo(){return new B;}}
// these classes' foo method returns via out args
class AA {void foo(out AA a){a = new AA;}}
class BB : AA {void foo(out BB b){b = new BB;}}
// Error: class BB use of AA.foo(out AA a) hidden by BB is
deprecated
void main() {
// these work
A a2, a = new A;
a2 = a.foo();
B b2, b = new B;
b2 = b.foo();
// these don't work because of the above error
AA aa2, aa = new AA;
aa.foo(aa2);
BB bb2, bb = new BB;
bb.foo(bb2);
}
---
Further examples and description of the implications can be found
in the bug report:
http://d.puremagic.com/issues/show_bug.cgi?id=7676
It is crucial for function subtyping, because functions are only
contravariant in their 'in' parameters, but covariant in their
'out' parameters and return type.
Function subtyping matters not only in classes with methods that
have 'out' parameters but also when assigning functions to
function pointers or passing functions as argument to other
functions.
I don't think this counts as an enhancement, it's definitely a
design bug, and there is no reason why it should not be fixed now.
This should not be harder to implement than covariance for return
types (which is already implemented) and wouldn't break any code.
More information about the Digitalmars-d
mailing list