gpt4 book ai didi

adt - 如何使用 Value 构造函数在 PureScript 中创建记录

转载 作者:行者123 更新时间:2023-12-02 05:30:22 24 4
gpt4 key购买 nike

我正在尝试根据数据数组创建一条记录,该函数如下所示:

type Address = {
street :: String,
city :: String,
state :: String
}

convertToAddress :: Array String -> Maybe Address
convertToAddress [street, city, state] = Just (Address { street: street, city: city, state: state })
convertToAddress _ = Nothing

在这里,我尝试使用 Address 值构造函数创建 Address 类型的记录,但在编译时会抛出错误:

Unknown data constructor Address

最佳答案

type 仅定义类型别名,因此 Address

{
street :: String,
city :: String,
state :: String
}

实际上是同一类型。如果你想生成一个构造函数,你必须使用newtype:

newtype Address = Address {
street :: String,
city :: String,
state :: String
}

或者,您可以删除代码中的构造函数并仅使用记录类型:

convertToAddress :: Array String -> Maybe Address
convertToAddress [street, city, state] = Just { street: street, city: city, state: state }
convertToAddress _ = Nothing

关于adt - 如何使用 Value 构造函数在 PureScript 中创建记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46405242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com