www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use a non-static objects in string `mixin`?

reply hype_editor <onetestdsacc yandex.ru> writes:
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?
Aug 27 2022
next sibling parent Paul Backus <snarwin gmail.com> writes:
On Saturday, 27 August 2022 at 13:20:13 UTC, hype_editor wrote:
 I need to use function `eval` sometimes, but compiler throws an 
 error: `Error: variable `firstOperand` cannot be read at 
 compile time`.
 ```d
 	override public double eval()
 	{
 		double firstOperand = firstExpr.eval();
 		double secondOperand = secondExpr.eval();

 		return mixin(
 			format("%d %s %d", firstOperand, operator, secondOperand)
 		);
 	}
 ```
`mixin` is not the right tool to use here. Try rewriting the code to use a `switch` statement or a series of `if`-`else` statements instead.
Aug 27 2022
prev sibling parent Dukc <ajieskola gmail.com> writes:
On Saturday, 27 August 2022 at 13:20:13 UTC, hype_editor wrote:
 I need to use function `eval` sometimes, but compiler throws an 
 error: `Error: variable `firstOperand` cannot be read at 
 compile time`.
You're probably misunderstanding `mixin`. It does not work like an eval function at Lisp or JavaScript or such. Instead, it evaluates it's contents at compile time, meaning that you can only use compile-time data in it, `enum` variables and template arguments for example. Because the operator is not known at compile time, this means you need something else. Switch statement Paul Backus suggested is one option. You could alternatively try an associative array that maps the operators to the respective functions, something like (untested): ```D enum opMap = [ "+": (double a, double b) => a+b, "-": (double a, double b) => a-b, //... ]; //in the eval function return opMap[operator](firstOperand, secondOperand); ```
Aug 29 2022