简体   繁体   中英

Dhall Association List

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, eg

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" }
    ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM