Skip to content

Union types #957

@Fell-x27

Description

@Fell-x27

What is your idea? Provide a use case.

We need union types like Haskell provides. For example, I want to extract output values from a list. It can be List<Input> or List<Output>. If I write a unified function like this:

fn extract_utxos(items: List<a>) -> a {
  let utxos =
    when list.head(items) is {
      Some(Input { .. }) -> todo @"extract outputs"
      Some(Output { .. }) -> todo @"use as is"
    }
  todo @"rest"
}

I'll get an error:
image

So, I can't determine which type I use. It would be a good idea to check a if it is Input or Output, but Aiken doesn't allow it.

Why is it a good idea?

If we had the ability to do something like:

fn extract_utxos(items: List<Input|Output>) -> a

it would be amazing. It would open up more possibilities to write cleaner, more abstract, and more readable code.

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

The current alternative is defining a custom type, such as:

type InOut {
  In(List<Input>)
  Out(List<Output>)
}

And then match it with your value, but that's not the goal we want to achieve by refactoring and extracting functions. There should be less code and fewer entities, not more. So, it is not good enough.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    🚀 Released

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions