What should delegates with qualifiers mean?
Q. Schroll
qs.il.paperinik at gmail.com
Wed Mar 24 19:23:15 UTC 2021
As per the grammar, a delegate can carry member function
attributes, including qualifiers (const, immutable) that
seemingly apply to the context. The spec does not speak about
this.
We should give this meaning!
The best way to approach this mentally is through dg =
&obj.method because the version where the delegate is from
binding variables is more difficult.
If obj is const or immutable, &obj.method should yield a
delegate(...) const or delegate(...) immutable. Intuitively,
* const means that the function pointed to will not change the
object.
* immutable means that the context cannot change (implying that
that the function pointed to will not change the object).
* mutable, i.e. no annotation, means that the context could
change by calling the pointed-to function or through other
actions.
While everywhere else, immutable is a requirement and a
guarantee, here it is a guarantee only. That is because the
context object cannot be reassigned independently of the function
pointer. Therefore, a `delegate() immutable` is convertible to a
`delegate() const` delegate which in turn is convertible to a
`delegate()`.
One benefit of fixing this is that pure immutable delegates are
referentially transparent while pure const ones almost aren't and
pure mutable ones carry almost no guarantees.
For delegates that come from binding local values, if all those
values are immutable, the delegate can be inferred immutable; if
all those values are const, the delegate can be inferred const.
I had a conversation with Timon Gehr about this and I think this
needs a little more attention.
More information about the Digitalmars-d
mailing list