On Thursday, 14 March 2013 at 03:45:39 UTC, kenji hara wrote:
2013/3/14 deadalnix <[email protected]>

It is a tempting idea, but do not work. Consider :

void delegate() immutable a;
const void delegate() b = a;

This would be forbidden as covariance of function parameters and
transitivity act in opposite directions.


You are misunderstanding about delegate type syntax and transitivity.

1. This qualifier for delegate type should be added after parameter types.

const void delegate() b; // bad, typeof(b) == const(void deelgate())
void delegate() const b;   // good, b can hold const method


After or before don't change anything here as type qualifier are transitives. It means that a const delegate must have a const context. Or, in code :

static assert(is(const delegate() == const delegate() const)); // Pass

2. A delegate which has immutable context cannot implicitly convertible to
const context delegate.

In general, if a delegate can implicitly convertible to another, there
should be _contravariance_ of parameter types.

void delegate() immutable a;
void delegate() const b;
b = a;  // bad
a = b;  // good


As showed above, this is equivalent to my sample code in the previous post, and it does break transitivity.

void delegate() const b; // Context of b is const (ie mutable or immutable).
void delegate() immutable a; // Context of a is immutable
a = b; // a now contains a reference to possibly mutable data considered as immutable by the type system.

This is *VERY* wrong.

Reply via email to