nextPermutation: why possible for dchar but not for char?

Ivan Kazmenko gassa at mail.ru
Sat Dec 28 15:38:34 PST 2013


On Saturday, 28 December 2013 at 23:05:54 UTC, monarch_dodra 
wrote:
>> 1. This is a minimal example of trying the permutations of a 
>> character array.
>>
>> -----
>> import std.algorithm;
>> void main () {
>>    char [] a;
>>    do { } while (nextPermutation(a));
>> }
>> -----
>>
>> This gives a compile error.  However, it works when I change 
>> "char [] a" to "dchar [] a".  Why?
>
> Because next permutation (AFAIK) works on ranges with 
> *assignable* elements, and "char[]" is not such a range: It is 
> a read-only range of dchars.

Ouch, is it an exception hard-coded into the language itself?  I 
thought it's just the nextPermutation's parameter type 
restrictions which don't allow "char []"...

>> 2. Why does nextPermutation hang up for empty arrays?  I 
>> suppose that's a bug?
>
> I suppose so. Please file it. If it is deemed "illegal", at the 
> very least, it should throw.

OK, will do that tomorrow (on Sunday).

>> Also, the example at
>> http://dlang.org/phobos/std_algorithm.html#nextPermutation
>> is wrong:
>> while (nextPermutation(a)) { }
>> should in fact be
>> do { } while (nextPermutation(a));
>> as above, or we miss the very first permutation.
>
> Noted. I'll try to fix that in the comming days. Or if anybody 
> else submits the pull, I'll merge it (*hint* *hint*, *wink*)

Hmm, I'll look into how hard is that for a start...

Ivan Kazmenko.


More information about the Digitalmars-d-learn mailing list