[Issue 7543] inout opApply should work properly

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Mar 9 14:27:38 PST 2012


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



--- Comment #6 from Boscop <kingboscop at gmail.com> 2012-03-09 14:27:43 PST ---
(In reply to comment #5)
> (In reply to comment #4)
> > (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:
> > [snip.]
> 
> I think you may misunderstand this issue.
> 
> > 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))
> > 
> 
> No, it does not. This is not sound.
> 
> class A{immutable(A) get(){return null;}}
> class B:A{
>     immutable(A) other;
>     this(immutable(A) other){this.other = other;}
>     override immutable(A) get(){return other;}
> }
> 
> A delegate(inout(A)) dg = (immutable(A) a) => new B(a);
> A a = new A;
> A x=dg(a);
> immutable(A) b = x.get();
> 
> // now a is mutable, b is immutable and (a is b).

Your example is unsound (no offense :)

You can't do:
A delegate(inout(A)) dg = (immutable(A) a) => new B(a);
and neither:
void function(inout(int)*) wfp = function(int*)(*p = 1;} // as you did in
comment 2 of bug 7542

1. as I wrote in bug 7542:
Given (R2 <: R1)
R2 delegate(inout(T)) <: R1 delegate(immutable(T))
(because of argument contravariance and immutable(T) <: inout(T)).
You can't assign an instance of a supertype to an instance of a subtype.

2. What do you expect from that func ptr? That you can assign a function that
takes mutable, const or immutable values as args?
You should not be allowed to assign a function that takes an immutable arg
because it assumes it won't be modified outside (and you could be passing in
mutable/const args)!
And you can't assign a function that takes a mutable because it could change
the const arg!
So you are left with being able to assign functions that take a const arg and
those that take an inout arg, because they can be substituted for the former.

Thats why you'd use a pointer to a function taking a const arg! (see bug 7542)
void function(const(int)*) wfp;
and then you can also assign a void function(inout(int)*)
You can call wfp with mutable, const and immutable args because as I wrote in
comment 4 of bug 7542:
T <: const(T)
immutable(T) <: const(T)

3. Aside from all the above, inout already has different semantics inside
function bodies, so it would be ambiguous to have another instance of inout in
a variable declaration because you can only refer to one constness (that of the
args of the current function).
Consider:
void main() {
    class A {}
    inout(A) foo(inout(A) a) {
        inout(A) b = a; // inout already depends on a's constness
        // can't introduce a different inout like you want here
        inout(A) delegate(inout(A)) dg = (immutable(A) a) => new immutable(A);
        return b;
    }
    foo(new A);
    foo(new const(A));
    foo(new immutable(A));
}

-- 
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