HeliosLang / compiler

Helios is a DSL for writing Cardano smart contracts. This library lets you compile Helios scripts and build Cardano transactions.
https://www.hyperion-bt.org/helios-book
BSD 3-Clause "New" or "Revised" License
139 stars 30 forks source link

switching on enum for Data type doesn't work as documented #109

Open rjharmon opened 10 months ago

rjharmon commented 10 months ago
  foo : Data = ...
  foo.switch{ MyEnum => ... }

Error: MyEnum isn't a valid enum member of Data

foo.switch{ (index: Int, fields: Data[]) => ... } works.

Note: foo.switch{ (index: Int, _) => ... } gives a syntax error. Not sure if that's as intended.

christianschmitz commented 10 months ago

This syntax error is due to the parser currently expecting at least one of Int, []Data, Map[Data]Data or ByteArray as an additional switch case in order to detect data-switch vs regular switch. I guess that doesn't work in case you're not interested in any of those cases and just want to look at enums and enum variants. I'll need some time to come up with a consistent way to handle this edge-case.

For the note: the syntax is sadly not perfectly consistent with the rest of the language, but the following should work:

foo.switch{(index: Int, []Data) => ...}
christianschmitz commented 10 months ago

Soon I'll expose the auto-generated from_data_safe method though, which might be a better fit for your use-case:

MyEnum::from_data_safe(data) -> Option[MyEnum]