3

I'm trying to wrap my head around how programming works in Dhall. Consider the following union type:

let Object = < NoId | WithId : Text >

I want to write a function extractId that returns an Optional Text containing the Id (or None), but I can't find a way to do the destructuring in Dhall.

Julian Stecklina
  • 1,271
  • 1
  • 10
  • 24

1 Answers1

4

The answer is to use the merge function. It expects a set with one handler function for each constructor of the union type. So to turn the above example union into an Optional Text, we can do:

let someObject = Object.NoId

let handlers = { NoId = None Text, WithId = λ(t : Text) → Some t }

in  merge handlers someObject

Julian Stecklina
  • 1,271
  • 1
  • 10
  • 24