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?




Re: Converting JSONValue to AssociativeArray.

2022-08-02 Thread hype_editor via Digitalmars-d-learn
On Monday, 1 August 2022 at 22:47:03 UTC, Steven Schveighoffer 
wrote:

On 8/1/22 2:00 PM, hype_editor wrote:

[...]


```d
// option 1
string[string] aa_data;
foreach(string k, v; data) {
aa_data[k] = v.get!string;
}
// option 2
import std.algorithm : map;
import std.array : assocArray;
import std.typecons : tuple;
auto aa_data = data.object
.byKeyValue
.map!(kv => tuple(kv.key, kv.value.get!string))
.assocArray;
```

I personally prefer the straightforward loop.

-Steve


Steve, thank you very much, works fine!


Converting JSONValue to AssociativeArray.

2022-08-01 Thread hype_editor via Digitalmars-d-learn
I need to convert variable of type `JSONValue` to variable of 
type `string[string]` (AssociativeArray).


```d
import std.json : JSONValue;
import std.stdio : writefln;

void main()
{
JSONValue data = parseJSON(`{ "name": "Hype Editor", "hobby": 
"Programming" }`);

writefln("%s", data);

auto aa_data = /+ Converting +/;
writefln("%s", aa_data);
}
```
And output will be:
```bash
["name":"Hype Editor","hobby":"Programming"]
```

**How can I do this in D?**