【问题标题】:How is the | symbol read in Elm?怎么样 | Elm 中读取的符号?
【发布时间】:2017-02-28 23:19:22
【问题描述】:

考虑以下示例代码:

-- 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

在这个例子中应该如何阅读|,通常在 Elm 中?

我熟悉它在集合构建符号中的“where”/“such that”,在 Haskell 的列表推导中,它具有非常相似的目的,例如

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

逻辑上等价于

来源: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

【问题讨论】:

  • 我不知道它有多官方,但我总是说“在哪里”。例如,姓名为 George 的人。
  • @Joe 但是guide.elm-lang.org 中的type Msg = Increment | Decrement 之类的呢?
  • 上下文很重要。在那种情况下,我说“或”。类型 Msg 是增量或减量。不知道为什么这没有在第一条评论上发布。
  • 它只是一个分隔符,你不要读它。列举项目(例如苹果、香蕉和橙子)时,逗号也不发音。
  • @JanTojnar 谢谢。我不确定为什么文档中没有明确的解释。

标签: syntax elm


【解决方案1】:

在类型中,我将其读作“”。在反例中:

type Msg = Increment | Decrement

我会将其解读为“MsgIncrement Decrement”。在Result 类型的一个稍微复杂但仍然常见的示例中:

type Result error value
    = Ok value
    | Err error

我会读到“ResultOkvalue Errerror”。

在您给出的记录更新语法示例中,我会将其解读为“with”而不是“where”。例如:

{ person | name = "George" }

是“personwithname 字段设置为 "George"”(而不是“where name = 'George'”暗示您正在根据person 中的值进行过滤)。不过,我认为这个比 type case 更模棱两可。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 2020-05-19
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多