I think immutable could benefit from a Value Range Propagation-like uniqueness
logic:

string a;
char[] b;

string c = a ~ b; // result of ~ is always unique
string z = c.dup; // also any kind of .dup is definitely unique
char[] q = z.dup; // also okay -- assign to a non-immutable

The lines w/ dup or ~ could be considered legal, since these ops produce
definitely unique values.  A handful
of specific actions known to produce unique values would produce temporaries
with a 'uniqueness' property, which
allows them to go either way toward mutable or immutable.  This list includes
(malloc, new, ~, .dup) but maybe
others.

Once an expression is assigned to a variable it becomes immutable or mutable
as per that variable's traits.

As an extension variables could be labeled as 'unique' which essentially means
they contain a value that is not
shared.  Unique would only be legal in cases where escape analysis can easily
prove that the value does not
get copied into both immutable and mutable variables (see below about
portability).

You could label a function as producing unique values -- only legal if the
value returned is definitely unique.
Again, if the language cannot do escape analysis then the user will be
required to do a defensive dup.

Portability note:  For portability reasons it is better to have well defined
escape analysis rules that can be
implemented by any compiler -- so a smart compiler might actually know there
is no escape but still be required
to forbid a particular expression since it breaks the rules.

Kevin

Reply via email to