Eliminate redundancy of dup/idup
nazriel
spam at dzfl.pl
Sun Sep 9 08:34:45 PDT 2012
On Sunday, 9 September 2012 at 15:32:01 UTC, kenji hara wrote:
> I've posted two pull requests for the purpose.
>
> https://github.com/D-Programming-Language/dmd/pull/1110
> https://github.com/D-Programming-Language/druntime/pull/298
>
> inout parameter and strongly purity function can realize this.
>
> Details:
>
> The new dup function's signature is:
>
> auto dup(E)(inout(E)[] arr) pure @trusted;
>
> If E has some mutable indirections (class, struct has mutable
> pointers, array, etc),
> dup would return inout(E). And the purity of dup function is
> calculated to 'constant purity'.
> Then the elements of returned value keep original type modifier.
>
> class C {}
> struct S { int* ptr; }
>
> C[] carr;
> S[] sarr;
> int[][] aarr;
> static assert(is(typeof(dup(carr)) == C[]));
> static assert(is(typeof(dup(sarr)) == S[]));
> static assert(is(typeof(dup(aarr)) == int[][]));
>
> If E does not have mutable indirection (built-in types, struct
> don't
> have pointers, etc),
> dup would return E[]. And the purity of dup function is
> calculated to
> 'strong purity'.
> Then returned value can be implicitly convertible to
> immutable(E[]).
>
> struct S { long value; }
>
> S[] sarr;
> int[] narr;
> static assert(is(typeof(dup(sarr)) == S[]));
> static assert(is(typeof(dup(parr)) == int*[]));
> immutable S[] isarr = dup(sarr); // allowed!
> immutable int[] inarr = dup(narr); // allowed!
>
> And today, dup function is used with UFCS.
>
> int[] marr;
> immutable int[] iarr = marr.dup();
>
> Finally, built-in dup and idup are merged into library dup().
> Destroy!
>
> Kenji Hara
Does anybody told you already that you freaking amazing?!
This rox, I've already seen it on GH.
Kenji for president++!
More information about the Digitalmars-d
mailing list