Open kjczarne opened 4 years ago
Any news on this one? Am I misunderstanding something conceptually?
@kjczarne Thanks for reporting! I found out that a simple comparison of an enum value converted to integer using to_i
and an integer literal is enough to reveal the bug:
meta:
id: csharp_enum_to_i
instances:
enum_eq_int:
value: 'fruit::apple.to_i == 2'
enums:
fruit:
2: apple
This translates to the following C# code:
_enumEqInt = (bool) (Fruit.Apple == 2);
which fails in the C# compiler with this error:
error CS0019: Operator `==' cannot be applied to operands of type `Kaitai.CsharpEnumToI.Fruit' and `int'
This points to the enumToInt
method implementation in CSharpTranslator
of the compiler, which apparently should do the typecasting:
override def enumToInt(v: expr, et: EnumType): String =
translate(v)
Gonna keep an eye on this one. Would submit a PR but my Scala skills are non-existent :(
Has anyone tried doing the typecasting directly as a workaround?
E.g.: instead of
fruit::apple.to_i == 2
try
fruit::apple.as<u4> == 2
or, if the above doesn't work, perhaps using both will trick it into casting the enum's value to something else:
fruit::apple.to_i.as<u4> == 2
This works as a workaround for me, though I'm using code from master so I am not sure if this is available in the release code.
I'll give it a try this weekend ;)
Hi again, seems like I've spotted another bug. Correct me if I'm wrong ;)
Given the following KSY snippet:
I get the following in C#:
I get the following errors:
To me, it seems like the
to_i
cast is missing from the switch cases and should look something like this: