【问题标题】:Dhall Association ListDhall 协会名单
【发布时间】:2019-03-30 09:09:19
【问题描述】:

我有一个联合类型,我希望一个字段成为关联列表。

let Blah = < First : { name : Text, params: XXX } | Second : { name : Text } >

在这里,我希望params 是一个关联列表,所以当我输入检查记录值时它会通过,例如

Blah.First { name = "Alex", params: [{ mapKey = "a", mapValue = 1 }] }

那么,XXX 应该在 Blah 中是什么类型?

【问题讨论】:

    标签: dhall


    【解决方案1】:

    答案取决于关联列表中存储的值的类型。在最一般的情况下,您可以在mapValue 的类型上对Blah 类型进行参数化,如下所示:

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

    如果您提前知道所需的mapValue 类型,则可以对其进行硬编码,而不是将Blah 设为某个类型的函数。或者,如果您打算多次将 Blah 用于同一个 mapValue 类型,您可以执行以下操作:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多