mixin templateReplaceable(){// returns a replaced thing when you use it as `with_someField`typeof(this)opDispatch(string s, T)(T replacement)if(s.length >5&& s[0..5]=="with_"){staticforeach(idx, member;typeof(this).tupleof){staticif(__traits(identifier, member)== s[5.. $])enum argIndex = idx;}// if the arg is not found you will get an ugly error message here about undefined// argIndex.... meh.returntypeof(this)(this.tupleof[0.. argIndex], replacement,this.tupleof[argIndex +1.. $]);}}
immutable struct Node {
string label;
Node* parentNode;int port;
mixin Replaceable;// add the ugly generic code from the top}voidmain(){
Node i =Node("one", null,4);
Node i2 = i.with_port(6);// and now use it like thisassert(i2.label =="one");assert(i2.port ==6);}