Immutable and unique in C#

Sönke Ludwig sludwig at outerproduct.org
Mon Nov 12 23:48:20 PST 2012


Am 13.11.2012 00:46, schrieb Walter Bright:
> On 11/9/2012 5:53 AM, Sönke Ludwig wrote:
>> Independent of this article I think D is currently missing out a lot by
>> omitting a proper unique type (a _proper_ library solution would be a
>> start, but I'm not sure if that can handle all details). It would make a
>> lot of the cases work that are currently simply not practical because of
>> loads of casts that are necessary.
> 
> Unique as a type qualifier comes with all kinds of subtle problems. For one thing, unique is more of
> a property of an expression rather than a property of the type.
> 
> But I was thinking - what if it's a storage class? Like ref is? Working through the use cases in my
> head, I think it can work. Unique being a property of an expression, this can be tested upon
> assignment to the unique variable. Upon reading that unique variable, the value of it is erased.
> 
> Function parameters can be qualified with "unique" as well, meaning their corresponding arguments
> can only be unique expressions.
> 

Currently I also see no reason why this should not be possible. Since it is acting recursively and
locally anyway, a storage class will probably capture almost everything that a full type would. I
had thought about the same route to reduce intrusiveness, before looking first what's possible with
a pure library solution.

The library solution seems surprisingly practical for some use cases. With the concept of "string"
and "weak" isolation (*), it can even handle the bridge between isolated and shared memory (thanks
to the completely separated nature of shared). By this, it solves a lot of the problems that only
Bartosz system was able to solve, for example declaring owned collections (no Mutex necessary),
which contain references to shared data, and still everything is safely checked.

A proper language feature can still do a lot more and IMO D should get there at some point - but I
think this is a viable short/mid-term alternative. I would like to get some discussion going to
eventually include something in phobos.


(*)

strong isolation: allows only strongly isolated and immutable references
 -> can be passed to other threads and casts implicitly to immutable

weak isolation: allows weakly isolated, immutable and shared references
 -> can only be passed to other threads


More information about the Digitalmars-d mailing list