Skip to content

Instantly share code, notes, and snippets.

@spott
Last active December 28, 2015 13:29
Show Gist options
  • Select an option

  • Save spott/7507709 to your computer and use it in GitHub Desktop.

Select an option

Save spott/7507709 to your computer and use it in GitHub Desktop.
compile time value addition. Won't compile, but I'm not sure why.
d_playground.d(38): Error: pure nested function 'a' cannot access mutable data 'this'
d_playground.d(38): Error: pure nested function 'a' cannot access mutable data 'this'
d_playground.d(14): Error: template instance d_playground.value.opBinary!("+", value).opBinary.intermediateValue!(plus, this, rhs) error instantiating
d_playground.d(47): instantiated from here: opBinary!("+", value)
d_playground.d(47): Error: template instance d_playground.value.opBinary!("+", value) error instantiating
struct value
{
int a;
//this(Op, A, B)( in intermediateValue!(Op, A, B) i) {
//a = i.a;
//}
//this( int b ) { a = b; }
const auto
opBinary(string op, T)(in T rhs) const pure {
static if (op == "+")
return intermediateValue!(value.plus,this,rhs)();
}
ref value opAssign(T)( in T t ) {
a = t.a;
return this;
}
static
int plus(T1, T2)(in T1 x, in T2 y) pure {
return x.a + y.a;
}
}
struct intermediateValue(alias Op, alias A, alias B)
{
auto opBinary(string op, T)(in T rhs) const pure {
static if (op == "+")
return intermediateValue!(value.plus,this,rhs)();
}
@property auto a() const pure {
return Op(A, B);
}
}
void test() {
value a = value(2);
value b = value(3);
value c;
c = a + b;
}
void main()
{
test();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment