Elm'的构造函数;s可扩展记录/Json。对象层次结构的解码
假设我有以下类型定义:Elm'的构造函数;s可扩展记录/Json。对象层次结构的解码,elm,Elm,假设我有以下类型定义: type alias EntityBase = { id: Int , name : String } -- Derived types type alias PersonSpecfic entityBase = { entityBase | age: Int , address : String } type alias Person = PersonSpecfic EntityBase
type alias EntityBase =
{ id: Int
, name : String
}
-- Derived types
type alias PersonSpecfic entityBase =
{ entityBase
| age: Int
, address : String
}
type alias Person = PersonSpecfic EntityBase
在当前版本的Elm(0.16)中,类型Person/PersonSpecfic
是否有任何构造函数?
(编译器说“找不到变量`PersonSpecific``
)
这与能够为类型层次结构创建Json解码器有关。是的,这看起来像是一个编译器错误。您始终可以创建自己的构造函数用于JSON解码:
createPerson:Int->String->Int->String->Person
createPerson id姓名年龄地址=
{id=id
,name=name
,年龄=年龄
,address=地址
}
自elm 0.16以来,语言中不再存在记录扩展名
部分原因是,对于以“通用”/“扩展”方式定义的记录类型,没有构造函数
可以在此线程上找到更多详细信息:
您发布的代码对我来说似乎运行良好:(share elm仅为elm 0.15,但我在本地使用0.16测试了相同的代码)@robertjlooby我发布的代码仅包含类型定义。通常,类型的名称也用作构造函数,但在“generic”/“extended”记录定义中不起作用。