On Wednesday, 1 April 2020 at 19:35:30 UTC, Net wrote:
from the below code, the expression "case [c]":void main() { import std.stdio, std.string, std.algorithm, std.conv; // Reduce the RPN expression using a stack readln.split.fold!((stack, op) { switch (op) { // Generate operator switch cases statically static foreach (c; "+-*/") case [c]: return stack[0 .. $ - 2] ~ mixin("stack[$ - 2] " ~ c ~ " stack[$ - 1]"); default: return stack ~ op.to!real; } })((real[]).init).writeln; }
the `static foreach (c; "+-*/")` operates on the characters, so c will be a char.
You use c in the case for the `switch (op)` where op is some char array (string), so if you want to check it in the switch, you will have to make c an array.
Here the example uses [c] but you could also `static foreach (c; ["+", "-", "*", "/"])` but I think [c] is a more elegant solution because it's done at compile time anyway. (case values are evaluated at compile time like if you would write enum x = [c];)
