www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Segmentation fault on sort

reply "Dfr" <deflexor yandex.ru> writes:
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 ?
Jan 03 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Jan 03 2014
parent reply "Dfr" <deflexor yandex.ru> writes:
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
Jan 04 2014
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Saturday, 4 January 2014 at 08:00:53 UTC, Dfr wrote:
 I just thought that "Color" is struct, so it is value type and 
 should be sorted like tuple without extra handwork, isn't it ?
Unfortunately, no. You need to add an opCmp to your struct.
 So how i understood it: 'sort' is old buggy sort, but when 
 added parens 'sort()' - now this is nice phobos sort, correct ?
Yes. The idea is that it should be (will be) deprecated. Pretty soon, using it will lead to a deprecation warning, and then will be removed outright. Once removed outright, then calling it without parens will call the correct std.algorithm.sort, "parentless-style"
Jan 04 2014