Thread overview | ||||||
---|---|---|---|---|---|---|
|
January 03, 2014 Segmentation fault on sort | ||||
---|---|---|---|---|
| ||||
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 ? |
January 03, 2014 Re: Segmentation fault on sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dfr | 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
|
January 04, 2014 Re: Segmentation fault on sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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
|
January 04, 2014 Re: Segmentation fault on sort | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dfr | 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" |
Copyright © 1999-2021 by the D Language Foundation