Segmentation fault on sort

Dfr deflexor at yandex.ru
Sat Jan 4 00:00:36 PST 2014


I just thought that "Color" is struct, so it is value type and 
should be sorted like tuple without extra handwork, isn't it ?

So how i understood it: 'sort' is old buggy sort, but when added 
parens 'sort()' - now this is nice phobos sort, correct ?


> Dfr:
>
>> struct Color {
>>  string fg;
>>  string bg;
>>  string attrs;
>>  int slid;
>> }
>>
>> auto tt = [Tuple!(uint, Color)(28, Color("0", "255", "", 0)), 
>> Tuple!(uint, Color)(28, Color("0", "255", "", 0))];
>> tt.sort;
>>
>> This just dies on "tt.sort", any idea what is wrong ?
>
> Your code has two problems: you are using the buggy built-in 
> sort instead of the Phobos one, and you have not defined an 
> opCmp and opEquals in Color.
>
> If you don't want to define those two methods you can use a 
> tuple again:
>
>
> import std.stdio, std.algorithm, std.typecons;
>
> struct Color0 {
>     string fg, bg, attrs;
>     int slid;
> }
>
> alias Color = Tuple!(string,"fg", string,"bg", string,"attrs", 
> int,"slid");
>
> void main() {
>     alias T = Tuple!(uint, Color);
>     auto tt = [T(28, Color("0", "255", "", 0)),
>                T(28, Color("0", "255", "", 0))];
>
>     tt.writeln;
>     tt.sort(); // Phobos sort.
>     tt.writeln;
> }
>
>
> Bye,
> bearophile



More information about the Digitalmars-d-learn mailing list