[Issue 7542] inout parameter contravariant should be allowed

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Mar 9 11:18:13 PST 2012


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



--- Comment #5 from Boscop <kingboscop at gmail.com> 2012-03-09 11:18:18 PST ---
(In reply to comment #4)
> void fooM(int*);
> void fooC(const(int)*);
> void fooI(immutable(int)*);
> void function(const(int)*) wfp;
> wfp = &fooM;
> wfp = &fooC;
> wfp = &fooI;

Sorry, this was wrong, that would allow modifying the arg in fooM. And it
wouldn't work because int* is not a supertype of const(int)* but a subtype.
If you want the arg to be mutable, you'd want to do:
void fooM(int*);
void fooC(const(int)*);
void fooI(immutable(int)*);
void function(int*) wfp;
wfp = &fooM;
wfp = &fooC;
wfp = &fooI;
The last case doesn't work because immutable(T) is not a supertype of T.
But if you have a
void fooIO(inout(int)*);
you could do
wfp = &fooIO;
because inout(T) is a supertype of T.

If you want
void function(const(int)*) wfp;
only these work:
wfp = &fooC;
wfp = &fooIO;

fooM doesn't work because T is not a supertype of const(T)
Makes sense because you don't want to modify a const object.
fooI doesn't work because immutable(T) is not a supertype of const(T)
Makes sense because immutable functions assume that the arg is not modified
elsewhere.

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