How to use a non-static objects in string `mixin`?

hype_editor onetestdsacc at yandex.ru
Sat Aug 27 13:20:13 UTC 2022


I need to use function `eval` sometimes, but compiler throws an 
error: `Error: variable `firstOperand` cannot be read at compile 
time`.
```d
import std.array;
import std.format;

public class BinaryOperatorExpression
{
	private
	{
		string operator;
		Expression firstExpr;
		Expression secondExpr;
	}

	public final this(T)(string operator, T firstExpr, T secondExpr)
	{
		this.operator = operator;
		this.firstExpr = firstExpr;
		this.secondExpr = secondExpr;
	}

	override public double eval()
	{
		double firstOperand = firstExpr.eval();
		double secondOperand = secondExpr.eval();

		return mixin(
			format("%d %s %d", firstOperand, operator, secondOperand)
		);
	}
}
```

Type `T` exactly has `eval` returning `double'.
How can I make `firstOperand` and `secondOperand` static?




More information about the Digitalmars-d-learn mailing list