Say I have the enum:
enum Item {
Sword(damage:Int);
Shield:
None;
}
How would I go about automatically generating a switch like this:
switch(value) {
case Sword(damage):
case Shield:
case None:
}
The closest I got to generating a case was this:
var sw = macro {switch($value) {
case None:
case _:
}
}
switch (sw.expr) {
case EBlock(exprs):
switch (exprs[0].expr) {
case ESwitch(e, cases, edef):
cases.push({
values: {values: [{pos: Context.currentPos, expr: EConst(CIdent("Shield"))}]}
})
case _:
}
case _:
}
But I get an error saying that the case is unused. I assume it has something with the position being wrong because I have no idea how that works. currentPos()
returns {file:"Main.hx"}'
so I assume that to get the correct position you need to do something like PositionTools.make()
. I would really appreciate some pointers. Maybe there is a way to do this with a little more expression reification?
Thanks in advance!