Re: opCmp with structs

2015-11-10 Thread anonymous via Digitalmars-d-learn
On 07.11.2015 16:59, anonymous wrote: Wat. It even compiles with @safe. That's not good. Filed an issue: https://issues.dlang.org/show_bug.cgi?id=15315

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
On Saturday, 7 November 2015 at 14:36:25 UTC, Mike Parker wrote: So my general question is: why immutable variables shouldn't be able to be moved (inside an array)? To be pedantic, sort isn't actually moving anything. It's reassigning elements, i.e. a[1] = a[2]. The immutable member makes

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
On Saturday, 7 November 2015 at 00:19:56 UTC, Ali Çehreli wrote: Continuing from your hint: So, opCmp works but it is sort() that cannot move objects of ku around because of that immutable variable. So my general question is: why immutable variables shouldn't be able to be moved (inside an

Re: opCmp with structs

2015-11-07 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 7 November 2015 at 11:48:56 UTC, Alex wrote: So my general question is: why immutable variables shouldn't be able to be moved (inside an array)? To be pedantic, sort isn't actually moving anything. It's reassigning elements, i.e. a[1] = a[2]. The immutable member makes it

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
Ok, ok... I see the direction, but I still miss something: From the point of view of "Programming in D", chapter 33.3, "Immutability of the slice vs the elements": I don't want to have an immutable slice but immutable elements. And your answers imply that sorting not only modify the slice

Re: opCmp with structs

2015-11-07 Thread BBaz via Digitalmars-d-learn
On Saturday, 7 November 2015 at 10:15:25 UTC, Alex wrote: So the question remains, how to sort? Do you think removing the immutable property from an id is the semantically right way? Or do you have a hint, maybe something like, a dup copy with removing immutability and returning a new, sorted

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
On Saturday, 7 November 2015 at 10:24:03 UTC, BBaz wrote: void main() { ku*[] tt = [new ku(2),new ku(1)]; sort(tt); } Don't really like this ;) not because writeln(tt); doesn't work any more, but because I have to think about where to use pointers and where not...

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
Ok... found the solution. The technical at least. import std.algorithm; import std.range; void main(){ ku[] tt = [ku(2), ku(1)]; //writeln(tt); auto index3 = new size_t[tt.length]; makeIndex!("a.id < b.id")(tt, index3); auto ind = indexed(tt, index3);

Re: opCmp with structs

2015-11-07 Thread Alex via Digitalmars-d-learn
Found something useful, here: http://dlang.org/phobos/std_algorithm_sorting.html#makeIndex with that I can achieve the following void main(){ ku[] tt = [ku(2), ku(1)]; //writeln(tt); auto index3 = new size_t[tt.length]; makeIndex!("a.id < b.id")(tt, index3);

Re: opCmp with structs

2015-11-07 Thread anonymous via Digitalmars-d-learn
On 07.11.2015 15:36, Mike Parker wrote: It's actually possible to use move one instance into another, though: void main() { import std.algorithm : move; ku k1 = ku(1); ku k2 = ku(2); k2.move(k1); assert(k1.id == 2); } Wat. It even compiles with @safe. That's not good.

Re: opCmp with structs

2015-11-06 Thread BBaz via Digitalmars-d-learn
On Friday, 6 November 2015 at 22:55:15 UTC, Alex wrote: Ok... the question is not silly any more... without 'immutable' it works. So, what am I missing? sorry, again a forum bug that stripped my answer: sort() fails because in the template constraint `hasAssignableElements` fails.

Re: opCmp with structs

2015-11-06 Thread BBaz via Digitalmars-d-learn
On Friday, 6 November 2015 at 22:55:15 UTC, Alex wrote: Ok... the question is not silly any more... without 'immutable' it works. So, what am I missing? sort() fails because in the template constraint `hasAssignableElements

Re: opCmp with structs

2015-11-06 Thread Ali Çehreli via Digitalmars-d-learn
On 11/06/2015 02:54 PM, Alex wrote: > I'm sure I'm doing a silly mistake somewhere, but why this doesn't work? > import std.stdio; > import std.algorithm; > > struct ku > { > immutable int id; > alias id this; > > this(int i) > { > id = i; > } > > int

opCmp with structs

2015-11-06 Thread Alex via Digitalmars-d-learn
I'm sure I'm doing a silly mistake somewhere, but why this doesn't work? import std.stdio; import std.algorithm; struct ku { immutable int id; alias id this; this(int i) { id = i; } int opCmp(ref const ku rhs)

Re: opCmp with structs

2015-11-06 Thread Alex via Digitalmars-d-learn
Ok... the question is not silly any more... without 'immutable' it works. So, what am I missing?