Search code examples
structchainingd

D(2) Programming: chaining functions call on struct


My struct seems to be broken, I can't figure 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)

Solution

  • Structs are value types: when setStart and setEnd return this, they are actually returning a copy of the struct. Thus, the second setStart call operates on a temporary copy, which is discarded.

    You can get around this by returning &this (and changing the return value to FilterBoundary* appropriately). Just be careful, this may be unsafe: because structs can live on the stack, saving a pointer to it may cause it to become a dangling pointer, and accessing it can corrupt memory.