how to make the commented out lines work when they are uncommented
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Mon Nov 2 21:09:59 PST 2015
On 11/02/2015 08:43 PM, steven kladitis wrote:
> import std.stdio, std.algorithm, std.array, std.traits,std.range,
> std.typecons,std.complex;
>
> enum AreSortableArrayItems(T) = isMutable!T &&
> __traits(compiles, T.init < T.init) &&
> !isNarrowString!(T[]);
>
> void selectionSort(T)(T[] data) if (AreSortableArrayItems!T) {
> foreach (immutable i, ref d; data)
> data.drop(i).minPos[0].swap(d);
> }
> // char[] a4 = ['a', 'b'];
> // a4.selectionSort;
That doesn't compile because selectionSort() requires !isNarrowString
but char[] is a narrow string. If you are fine with sorting those bytes,
then try ubyte[] as the type. Otherwise, sorting chars is dubious
because char is a Unicode code unit, potentially a part of a Unicode
character.
> // auto a7 = [complex(1.0,0.0), complex(2.0,0.0)];
> // a7.selectionSort;
That fails because there is no default ordering between complex numbers.
(Ditto for a8.) (I haven't looked at the implementation of 'complex' but
I remember from Math that that doesn't make sense anyway.)
Ali
More information about the Digitalmars-d-learn
mailing list