Immutable and unique in C#

Walter Bright newshound2 at digitalmars.com
Tue Nov 13 17:28:08 PST 2012


On 11/13/2012 3:47 PM, Daniel Murphy wrote:
> "Walter Bright" <newshound2 at digitalmars.com> wrote in message
> news:k7t125$7on$1 at digitalmars.com...
>>
>> A Unique!T can only be initialized by:
>>
>>     1. a destructive read of another instance of Unique!T
>>     2. an expression that can be determined to generated an isolated
>> pointer
>>
>> #2 is the interesting bit. That requires some compiler support, sort of
>> like:
>>
>>       __unique(Expression)
>>
>> which will allow it through if Expression is some combination of new, pure
>> functions, and other Unique pointers.
>>
>
> This is somewhat possible in the current language.  The makeU function can
> be an arbitrarily complicated strongly pure function.
>
> void funcTakingUnique(U, T...)(U function(T) pure makeU, T args) if
> (is(typeof({ immutable i = makeU(args) }) // If it converts to immutable, it
> is either unique or immutable
> {
>      auto unique = makeU(args); // guaranteed to be unique/immutable
> }
>
> class A
> {
>      this(int a, string b) {}
> }
>
> void main()
> {
>      funcTakingUnique!A(A function(int a, string b) { return new A(a, b); },
> 4, "Awesome");
> }
>
>

This is a great insight.


More information about the Digitalmars-d mailing list