View mode: basic / threaded / horizontal-split · Log in · Help
September 09, 2012
Eliminate redundancy of dup/idup
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
September 09, 2012
Re: Eliminate redundancy of dup/idup
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++!
September 09, 2012
Re: Eliminate redundancy of dup/idup
On Sunday, 9 September 2012 at 15:32:01 UTC, kenji hara wrote:
> Then returned value can be implicitly convertible to 
> immutable(E[]).

What about rebindable with auto? For example:

void main() {
	char[] a = "cool".dup;
	string b = a.idup; // explicitly immutable(char)[]
	auto c = a.idup; // automatically typeof(c) == immutable(char)[]
	c = "another"; // so this compiles
}

I do this in a few places in my code. If I want it to be 
immutable(char[]), I'll write immutable instead of auto.
September 09, 2012
Re: Eliminate redundancy of dup/idup
Thanks for quick response.

2012/9/10 nazriel <spam@dzfl.pl>:
> Does anybody told you already that you freaking amazing?!
> This rox, I've already seen it on GH.
>
> Kenji for president++!
>
September 09, 2012
Re: Eliminate redundancy of dup/idup
2012/9/10 Adam D. Ruppe <destructionator@gmail.com>:
> On Sunday, 9 September 2012 at 15:32:01 UTC, kenji hara wrote:
>>
>> Then returned value can be implicitly convertible to immutable(E[]).
>
>
> What about rebindable with auto? For example:
>
> void main() {
>         char[] a = "cool".dup;
>         string b = a.idup; // explicitly immutable(char)[]
>         auto c = a.idup; // automatically typeof(c) == immutable(char)[]

Yes. In this case, you should write:
         immutable c = a.dup(); // explicit immutable conversion

>         c = "another"; // so this compiles
> }
>
> I do this in a few places in my code. If I want it to be immutable(char[]),
> I'll write immutable instead of auto.

Kenji Hara
September 09, 2012
Re: Eliminate redundancy of dup/idup
On Sunday, 9 September 2012 at 15:32:01 UTC, kenji hara wrote:
> Finally, built-in dup and idup are merged into library dup(). 
> Destroy!
>
> Kenji Hara

Well, that is pretty awesome. I didn't see an example which says 
this would work, but I expect it would?

char[] a = dup("Hello");
string b = dup(a);

I'm not as familiar with the pure to immutable but I believe to 
immutable(char)[] is or should be valid right?
September 09, 2012
Re: Eliminate redundancy of dup/idup
On 09/09/2012 05:44 PM, kenji hara wrote:
> 2012/9/10 Adam D. Ruppe <destructionator@gmail.com>:
>> On Sunday, 9 September 2012 at 15:32:01 UTC, kenji hara wrote:
>>>
>>> Then returned value can be implicitly convertible to immutable(E[]).
>>
>>
>> What about rebindable with auto? For example:
>>
>> void main() {
>>          char[] a = "cool".dup;
>>          string b = a.idup; // explicitly immutable(char)[]
>>          auto c = a.idup; // automatically typeof(c) == immutable(char)[]
>
> Yes. In this case, you should write:
>            immutable c = a.dup(); // explicit immutable conversion
>
>>          c = "another"; // so this compiles

This does not compile anymore now.

>> }

+1 for moving to library and lifting restrictions on conversion.
-1 for removing .idup, it is handy.
September 09, 2012
Re: Eliminate redundancy of dup/idup
Yes, This works just as expected.

import std.stdio;
void main()
{
   char[] a = dup("Hello");
   string b = dup(a);
   writeln(a);  // prints "Hello"
   writeln(b);  // prints "Hello"
   assert(cast(void*)a.ptr !is cast(void*)b.ptr);  // elements are duplicated
}

Kenji Hara

2012/9/10 Jesse Phillips <jessekphillips+D@gmail.com>:
> On Sunday, 9 September 2012 at 15:32:01 UTC, kenji hara wrote:
>>
>> Finally, built-in dup and idup are merged into library dup(). Destroy!
>>
>> Kenji Hara
>
>
> Well, that is pretty awesome. I didn't see an example which says this would
> work, but I expect it would?
>
> char[] a = dup("Hello");
> string b = dup(a);
>
> I'm not as familiar with the pure to immutable but I believe to
> immutable(char)[] is or should be valid right?
September 09, 2012
Re: Eliminate redundancy of dup/idup
On 09/09/2012 05:32 PM, kenji hara wrote:
> ...
> Then the elements of returned value keep original type modifier.
> ...

Usually .dup or .idup are used to explicitly change the type modifier,
and they still allow type deduction.
I'd be fine with having .dup be inout, .mdup mutable and .idup
immutable.
September 09, 2012
Re: Eliminate redundancy of dup/idup
On Sunday, 9 September 2012 at 16:25:12 UTC, Timon Gehr wrote:
>>>         c = "another"; // so this compiles
>
> This does not compile anymore now.


One option I've heard that might be OK is to make auto strip off 
the top level immutable:

immutable(char[]) a;
auto b = a; // b is immutable(char)[]
b = "test"; // allowed


This kind of thing is already done on function calls.


If you want to keep the full immutability including the top one, 
use immutable instead of auto.
« First   ‹ Prev
1 2 3
Top | Discussion index | About this forum | D home