[Issue 3075] Implement parameter contravariance

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jan 9 14:29:25 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=3075

Bolpat <qs.il.paperinik at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |qs.il.paperinik at gmail.com

--- Comment #30 from Bolpat <qs.il.paperinik at gmail.com> ---
Overriding with contravariant parameter types can work.
Because D has overloading and contravariant overriding is factually
indistinguishable from overloading, an annotation must be used to clarify:

It could be as simple as giving override an optional type list that signifies
the types of the parameters of the method to be overridden:

Attribute:
    ...
    override
+   override Parameters

class X {}
class Y : X {}

class C
{
  void foo(Y y) {}
}

class D : C
{
  override(Y) void foo(X x) {} // overrides foo(Y y)
}

It's similar to the VisibilityAttribute package with a QualifiedIdentifier to
signify the exact package it should be visible from. It's very specific and one
could argue that overloading and forwarding solves the issue in almost all
cases.

class D : C
{
  override void foo(Y y) { foo(cast(X) y); }
  void foo(X x) { ... }
}

There might be very specific circumstances where this is not a viable option.

--


More information about the Digitalmars-d-bugs mailing list