以下代码是什么意思?
type Update
= First Field.Content
| Last Field.Content
| Email Field.Content
| Remail Field.Content
| Submit
(代码取自http://elm-lang.org/edit/examples/Intermediate/Form.elm第36行)
声明一个新类型Update
?这些竖条是什么意思?
答案 0 :(得分:1)
是的,这声明了一个新类型Update
。竖条可以读作"或"。也就是说,Update
类型的东西可以是:
First
,其中包含Field.Content
Last
,其中包含Field.Content
Email
,其中包含Field.Content
Remail
,其中包含Field.Content
Submit
,没有相应的数据。要处理Update
类型的值,您可以使用case
- of
语法来区分不同的可能值:
update : Update -> State -> State
update upd st = case upd of
First content -> st -- do something in the situation that the Update is a First
Last content -> st -- do something in the situation that the Update is a Last
Email content -> st -- do something in the situation that the Update is a Email
Remail content -> st -- do something in the situation that the Update is a Remail
Submit -> st -- do something in the situation that the Update is a Submit
我会在Elm网站上添加一个文档链接,但它正在重新编写新的0.14版本。我可以稍后回来编辑它;)