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

2022-08-29 Thread Dukc via Digitalmars-d-learn

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);
```


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

2022-08-27 Thread Paul Backus via Digitalmars-d-learn

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.


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

2022-08-27 Thread hype_editor via Digitalmars-d-learn
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?