cannot sort an array of char
via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Nov 6 05:55:42 PST 2014
On Thursday, 6 November 2014 at 10:52:32 UTC, Ivan Kazmenko wrote:
> On Wednesday, 5 November 2014 at 13:34:05 UTC, Marc Schütz
> wrote:
>> On Wednesday, 5 November 2014 at 12:54:03 UTC, Ivan Kazmenko
>> wrote:
>>> Hi!
>>>
>>> This gives an error (cannot deduce template function from
>>> argument types):
>>>
>>> -----
>>> import std.algorithm;
>>> void main () {
>>> char [] c;
>>> sort (c);
>>> }
>>> -----
>>>
>>> Why is "char []" so special that it can't be sorted?
>>>
>>> For example, if I know the array contains only ASCII
>>> characters, sorting it sounds no different to sorting an "int
>>> []".
>>
>> Hmm... this doesn't work either:
>>
>> import std.algorithm;
>> import std.utf;
>> void main () {
>> char [] c;
>> sort (c.byCodeUnit);
>> }
>>
>> But IMO it should.
>
> So, you imply that to use a char array as a RandomAccessRange,
> I have to use byCodeUnit? (and it should work, but doesn't?)
>
Yes. H.S. Teoh has already submitted a PR to fix it.
> Fine, but how does one learn that except by asking here?
> Googling did not produce meaningful results for me.
>
> For example, isRandomAccessRange[0] states the problem:
> -----
> Although char[] and wchar[] (as well as their qualified
> versions including string and wstring) are arrays,
> isRandomAccessRange yields false for them because they use
> variable-length encodings (UTF-8 and UTF-16 respectively).
> These types are bidirectional ranges only.
> -----
> but does not offer a solution. If (when) byCodeUnit does
> really provide a random-access range, it would be desirable to
> have it linked where the problem is stated.
>
> [0] http://dlang.org/phobos/std_range.html#.isRandomAccessRange
I agree. But how should it be implemented? We would have to
modify algorithms that require an RA range to also accept char[],
but then print an error message with the suggestion to use
byCodeUnit. I think that's not practicable. Any better ideas?
More information about the Digitalmars-d-learn
mailing list