You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are cases where we would like to do defensive copying only when a value is not const.
For example, an immutable list implementation might take a List as input. If the List is not const, it needs to make a defensive copy.
Currently only something that is itself const can be sure to receive const values. This is very restrictive. Lots of immutable collections, for example, use mutable internals for efficiency.
What is requested, then, is a way for non-const classes to know whether a value is const and behave differently. One feature that would satisfy this would be a way to specify an alternative constructor that is used when the values passed in are const:
classImmutableList<T> {
finalList<T> list;
// This will be called if `list` is `const`.ImmutableList(constList<T> list) :this.list = list;
// This will be called if `list` is not `const`. ImmutableList(List<T> list) :this.list = list.toList();
}
Further, 'auto const' would apply in these contexts, allowing:
ImmutableList([1, 2, 3])
to allocate a const list and avoid copying altogether.
The idea is just for illustration--the request is to give a way to avoid defensively copying const values.
Thanks!
The text was updated successfully, but these errors were encountered:
This issue would also be closely related to #125, because immutability also ensures that an instance can be shared freely. To make these two play well together, we would need to make sure, as far as possible, that the immutability of the value of a constant expression is known during static analysis.
I agree that a general notion of deeply immutable values, separate from const values, would probably be useful and solve some issues like this.
An immutable object could be any object created from a const constructor with only immutable arguments (that ensures that you can't introduce mutability) and some system created collections (like the result of List.unmodifiable when all elements are immutable).
If we had such a property reflected in the run-time values, then it would be possible to ask whether Object.isImmutable(anything).
There would still be no way to detect whether a list of mutable objects is itself unmodifiable. That's a logical property, not a physical one (one logical object may consist of multiple different physical objects, and the language can not be expected to know the logical boundaries), whereas deep immutability is a physical property of the object graph independent of logical boundaries.
(I worry when I see people using const for only the immutability, because they do also pay for the canonicalization and compile-time evaluation, if nothing else it restricts what you can put into the const object). If we had (run-time) canonicalization or immutability separate from const-ness, then some problems might be easier to solve.
There are cases where we would like to do defensive copying only when a value is not
const
.For example, an immutable list implementation might take a
List
as input. If theList
is notconst
, it needs to make a defensive copy.Currently only something that is itself
const
can be sure to receiveconst
values. This is very restrictive. Lots of immutable collections, for example, use mutable internals for efficiency.What is requested, then, is a way for non-
const
classes to know whether a value isconst
and behave differently. One feature that would satisfy this would be a way to specify an alternative constructor that is used when the values passed in areconst
:Further, 'auto const' would apply in these contexts, allowing:
to allocate a
const
list and avoid copying altogether.The idea is just for illustration--the request is to give a way to avoid defensively copying
const
values.Thanks!
The text was updated successfully, but these errors were encountered: