我可以指定类型是任意记录吗?
我有一个类型
type alias WithStatus l =
{ l
| status :
Status
}
This 使用状态字段扩展记录。
现在我想创建一个函数,它接受一条记录并赋予它特定的状态,所以我做了:
addStatus : Status -> l -> WithStatus l
addStatus status l =
{ l
| status =
status
}
然而 elm 抱怨 l
可能不是一条记录。
This is not a record, so it has no fields to update!
21|> { l
22| | status =
23| status
24| }
This `l` value is a:
l
But I need a record!
这是真的。然而,elm 也无法知道类型别名中的 l
是一条记录,它会在使用站点上进行检查。所以它在这里抱怨似乎有点奇怪。
有没有办法表达一个函数,它接受任何记录 l
并添加状态?
I have a type
type alias WithStatus l =
{ l
| status :
Status
}
This extends a record with a status field.
Now I'd like to make a function that takes a record and gives it a particular status, so I made:
addStatus : Status -> l -> WithStatus l
addStatus status l =
{ l
| status =
status
}
However elm complains that l
could potentially not be a record.
This is not a record, so it has no fields to update!
21|> { l
22| | status =
23| status
24| }
This `l` value is a:
l
But I need a record!
Which is true. However elm has no way of knowing that the l
in the type alias is a record either, it checks for that on the use site. So it seems a little odd that it's complaining here.
Is there a way to express a function which takes any record l
and adds a status?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以
替换现有状态,但以您希望的方式引入新字段的能力不再可能了。
它曾经是,从 0.7 到 0.16(2015 年发布),它被删除了,因为 < a href="https://elm-lang.org/news/compilers-as-assistants" rel="nofollow noreferrer">感觉实际上并非如此有用的:
You can do
which replaces an existing status, but the ability to introduce new fields in the way you were hoping isn't possible any more.
It used to be, from 0.7 until 0.16 (released in 2015) where it was removed because it wasn't felt to be actually all that useful: