Permutations of array (slice) ?

Paul Backus snarwin at gmail.com
Tue Dec 12 15:20:23 UTC 2023


On Tuesday, 12 December 2023 at 14:57:48 UTC, Kevin Bailey wrote:
> perm.d:8:26: error: none of the overloads of template 
> ‘std.algorithm.iteration.permutations’ are callable using 
> argument types ‘!()(char[])’
>     8 |         foreach (perm; as.permutations)
>       |                          ^
> /usr/lib/gcc/x86_64-linux-gnu/13/include/d/std/algorithm/iteration.d:7919:20: note: Candidate is: ‘permutations(Range)(Range r)’
>      with `Range = char[]`
>   must satisfy the following constraint:
> `       isRandomAccessRange!Range`
>  7919 | Permutations!Range permutations(Range)(Range r)
>       |                    ^

Because of autodecoding [1], slices of char and wchar are treated 
by Phobos as bidirectional ranges of dchar. (They are not 
random-access ranges because UTF-8 and UTF-16 are variable-length 
encodings.)

To work around this, use std.utf.byChar:

     import std.utf: byChar;
     foreach (perm; as.byChar.permutations)
         writeln(perm);

[1] 
http://ddili.org/ders/d.en/ranges.html#ix_ranges.decoding,%20automatic


More information about the Digitalmars-d-learn mailing list