I have a Union type that I wish one field to be an association list.
let Blah = < First : { name : Text, params: XXX } | Second : { name : Text } >
Here, I wish params
to be an association list so when I type check Record values it will pass, e.g.
Blah.First { name = "Alex", params: [{ mapKey = "a", mapValue = 1 }] }
So, what type should XXX
be in Blah
?
The answer depends on the type of value stored in the association list. In the most general case, you can parametrize the Blah
type on the type of the mapValue
, like this:
let Blah =
λ(a : Type)
→ < First :
{ name : Text, params : List { mapKey : Text, mapValue : a } }
| Second :
{ name : Text }
>
in (Blah Natural).First
{ name = "Alex", params = [ { mapKey = "a", mapValue = 1 } ] }
If you know the desired mapValue
type ahead of time, you can hard-code it instead of making Blah
a function of a type. Or, if you plan to use Blah
for the same mapValue
type multiple times, you can do something like this:
let Blah =
λ(a : Type)
→ < First :
{ name : Text, params : List { mapKey : Text, mapValue : a } }
| Second :
{ name : Text }
>
let Foo = Blah Natural
in [ Foo.First { name = "Alex", params = [ { mapKey = "a", mapValue = 1 } ] }
, Foo.Second { name = "John" }
]