types - "Nested"榆树中的联合类型

标签 types elm union-types

我正在学习 elm 并试图改变我对 TypeScript 类型系统的看法。我想知道使用这样的嵌套类型的最佳方式是什么:

type Player = X | O
type Cell = Player | Empty

viewCell: Cell -> string
viewCell cell = 
  case cell of
    X -> "X"
    O -> "O"
    Empty -> " "

编译器提示

The first pattern is trying to match `X` values of type:

    Player

But the expression between `case` and `of` is:

    Cell

我可以像这样更改 viewCell,但我不知道如何获取播放器

viewCell: Cell -> String
viewCell cell = 
  case cell of
    Player -> -- how to get the player ??
    Empty -> " "

问题不在于显示值本身,而是“解构”嵌套联合类型。我想稍后在这样的事情中使用它:

check: (List Cell) -> string
check three =
  case three of
    [X, X, X] -> "X won"
    [O, O, O] -> "O won"
    _ -> "still going"

这也给了我来自编译器的类似提示

最佳答案

type Cell = Player | Empty

Player 不是类型,而是 Cell 类型的值。但是,您也可以给它一个参数,在这种情况下,它将是一个值构造函数,当给定一个参数时,它返回一个 Cell 类型的值。所以在

type Player = X | O
type Cell = Player Player | Empty

Player Player 中的第一个 Player 本质上是一个函数,当给定一个 Player 类型的值时,它将返回一个 类型的值>单元格。或打字说Player -> Cell

另请注意,类型和构造函数可以具有相同的名称,因为它们位于不同的域中。它们并不冲突,因为它们指的是不同的东西,一个指的是类型,另一个指的是值(构造函数)。但事实上你可以并不一定意味着你应该这样做,因为它可能会非常困惑。

然后您可以在 Cell 和嵌套的 Player 上进行模式匹配,如下所示:

type Player = X | O
type Cell = Player Player | Empty

viewCell: Cell -> String
viewCell cell = 
  case cell of
    Player X -> "X"
    Player O -> "O"
    Empty -> " "

PlayerEmpty 这里指的是 Cell 的构造函数/变体,而不是类型。同样,XO 指的是 Player 的变体,它们也不是类型。

关于types - "Nested"榆树中的联合类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59682602/

相关文章:

c++ - 长整数和 float

c++ - 我应该使用哪个数组/列表?

typescript - 解决 Array<T> 和 IObservableArray<T> 不兼容性问题

ocaml - 如何为联合类型编写 reasonml 绑定(bind)

typescript - 在 TypeScript 中调用以联合作为其返回类型的函数后,如何获得 vscode 自动完成功能?

python - 获取给定值对应的 "zero value"

performance - 测量榆树性能

record - 在 Elm (0.18) 中更新记录内嵌套值的简洁方法

elm - Elm 中 Maybe a -> Svg Msg 类型的函数中没有 Nothing 时返回什么

typescript - 某些成分中的冲突类型