2017-02-28 46 views
4

考虑下面的示例代码:| |是怎么回事?在Elm读的符号?

-- Update the fields of a record. (It must have the fields already.) 
{ person | 
    name = "George" } 

-- Update multiple fields at once, using the current values. 
{ particle | 
    position = particle.position + particle.velocity, 
    velocity = particle.velocity + particle.acceleration } 

来源:Learn Elm in X Minutes

一个应该如何在这个例子中阅读|,并在榆树一般?我很熟悉它在set-builder表示法中是“where”/“such that”,而在Haskell的列表解析中它有一个非常类似的目的,例如,

[ x*2 | x <- [1..10] ]

是逻辑上等同于

enter image description here

来源:Learn You A Haskell

(当然我也熟悉它作为一元 “或” 运营商C使用类语言)

怎么样像type Msg = Increment | Decrement

来源:https://guide.elm-lang.org

或者,在这个例子中讨论Union Types时:

type Boolean 
    = T 
    | F 
    | Not Boolean 
    | And Boolean Boolean 
    | Or Boolean Boolean 
+1

我不知道它是如何官方,但我总是说“在哪里”。例如,名字等于乔治的人。 – Joe

+0

@Joe但是'type Msg = Increment |在https://指南中减量。elm-lang.org? –

+0

上下文很重要。在那种情况下,我会说“或”。类型Msg是递增或递减。不知道为什么这没有发表第一条评论。 – Joe

回答

9

在类型我把它读作 ''。在反例:

type Msg = Increment | Decrement 

我会读它为 “MsgIncrementDecrement”。在Result类型的一个稍微复杂,但仍然常见的例子:

type Result error value 
    = Ok value 
    | Err error 

我会读“一个Result要么是OkvalueErrerror”。

在你给的记录更新语法的例子,我将它读成“”而非“其中”。例如:

{ person | name = "George" } 

为“person价值name字段设置为"George"”(而不是“其中名称=‘乔治’”这似乎意味着,你滤波基于什么值在person)。尽管如此,我认为这个比模型案例更模糊。

+0

非常有帮助,谢谢! –