aiken-lang / aiken

A modern smart contract platform for Cardano
https://aiken-lang.org
Apache License 2.0
454 stars 84 forks source link

Provide a mechanism for non-terminating cast from Data #889

Closed KtorZ closed 2 months ago

KtorZ commented 5 months ago

What is your idea? Provide a use case.

We want to provide ways to safely cast between types similar to expect, but without crashing the entire program on failure.

This could be introduced via a new syntax, to not overload expect from its current semantic. Something along the lines of:

if unknown is Foo {
  ... 
} else if unknown is Bar {
  ...
} else {
  ...
}

where Foo and Bar are type annotations. Within each branch, the type of unknown would be respectively Foo, Bar and Data.

Why is it a good idea?

This is particularly useful for casting from an unknown type (i.e. Data) from within an iterator (e.g. foldr).

What is the current alternative and why is it not good enough?

Alternative right now requires a lot of boilerplate and the explicit use of builtin functions such as choose_data.