[Issue 7543] inout opApply should work properly

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Mar 9 10:24:48 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=7543


Boscop <kingboscop at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kingboscop at gmail.com


--- Comment #4 from Boscop <kingboscop at gmail.com> 2012-03-09 10:24:53 PST ---
(In reply to comment #3)
> We'll need a clear spec of what it means to have inout at two nesting levels of
> a function signature.

This follows naturally from variance rules:
In-arguments are contravariant, so you can pass anything whose type is a
subtype of the argument type. Then the usual rules of function subtyping apply
(e.g. see bug 7676)
So, if you have a function of type "R1 function(R2 delegate(inout(T1)) f)" you
can pass anything for f that is a subtype of "R2 delegate(inout(T1))". For
argument types to the delegate, the subtyping rules apply again.
Since in-arguments are contravariant (and return types (and types of out-args)
are covariant), any value of type "R delegate(T)" where (R <: R2 and inout(T1)
<: T) is a subtype of "R2 delegate(inout(T1))" (<: is the subtype relation).

Also the following subtyping rules for inout apply (R2 <: R1):
R2 delegate(inout(T)) <: R1 delegate(immutable(T))
R2 delegate(inout(T)) <: R1 delegate(const(T))
R2 delegate(inout(T)) <: R1 delegate(T)
And of course:
R2 delegate(inout(T)) <: R1 delegate(inout(T))

>From that follows (given two types A,B and B <: A):
B delegate(immutable(A)) <: A delegate(inout(A))

Which is exactly what we want. (Analogous for functions. Btw, functions should
be a subtype of delegates).

Example:
class A {}
class B : A {}
void foo(A function(A function(inout(A))) dg);
B bar(A function(inout(A)) f);
B baz(inout(A) a);
foo(bar(baz(new immutable(A))));
foo(bar(baz(new const(A))));
foo(bar(baz(new A)));

so it all works out nicely...

To make this work in DMD, it has to support function subtyping according to
variance rules. It's important that functions with an inout arg are subtypes of
functions with an immutable/const/mutable arg (all other arg types / return
type being equal) because the former can be substituted for one of the latter.

(This issue is related to bug 7542.)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list