D (2) Programming: function chaining calls struct

My structure seems to be broken, I can't figure out why:

struct FilterBoundary {

private uint start;
private uint end;

public static immutable uint MIN = 0; 
public static immutable uint MAX = uint.max;

public this(uint start=0, uint end=0){
    checkRange(start,end);
    this.start=start;
    this.end=end;
}

public uint getStart(){
    return this.start;
}

public uint getEnd(){
    return this.end;
}

private void checkRange(uint start, uint end){
    if(start>end){
        throw new Exception("Invalid range.");
    }
}

public FilterBoundary setStart(uint start){
    checkRange(start,this.end);
    this.start=start;
    return this;
}

public FilterBoundary setEnd(uint end){
    checkRange(this.start,end);
    this.end=end;
    return this;
}
}

      

This code

auto r1 = FilterBoundary(6, 7);

//Correct
writeln(r1);

r1.setStart(5);
//Correct
writeln(r1);

//Wrong end set to 9 but start stays to 5
r1.setEnd(9).setStart(2);
writeln(r1);

      

Produces this output:

FilterBoundary(6, 7) 
FilterBoundary(5, 7) 
FilterBoundary(5, 9)

      

+3


source to share


1 answer


Structures are value types: when setStart

and setEnd

return this

, they actually return a copy of the structure. So the second call setStart

runs on a temporary copy that is discarded.



You can work around this by returning &this

(and changing the return value to FilterBoundary*

) accordingly . Just be careful, it can be unsafe: since structs can live on the stack, storing a pointer to it can cause it to become a dangling pointer, and accessing it can corrupt memory.

+3


source







All Articles