【问题标题】:How to use a defined type inside a class as a val in Ocaml?如何在 Ocaml 中使用类中定义的类型作为 val?
【发布时间】:2019-05-28 20:56:04
【问题描述】:

我正在编写一个存储定义类型数组的类。我的代码语法有问题-我认为。我想在数组中获得一个定义的类型,但我的代码一直出错。

我已经创建了一个类,写下我需要的数组。我已经尝试过以这些方式编写它;


[|(Rook, Black, (Alive of (A, 1)))]
[|(Rook, Black, (Alive of A, 1))]
[|(Rook, Black, Alive of (A,1))]
[|(Rook, Black, Alive of A,1)]
[|(Rook, Black, (A,1))

但遗憾的是,它将我的数组显示为chess_piece * chess_colour * (chess_letter * int),否则会出现操作错误。

这是我定义的类型

type chess_letter = A | B | C | D | E | F | G | H
and chess_piece = King | Queen | Rook | Bishop | Knight | Pawn
and chess_colour = Black | White 
and chess_position = Alive of chess_letter * int | Dead

数组应包含[|(chess_piece, chess_colour, chess_position)|]

【问题讨论】:

  • 这不是 OOP,所以我删除了标签。您发布的代码中没有类或对象。

标签: syntax ocaml variant


【解决方案1】:

构造变量Alive的值的语法是:

Alive (A, 1)

of 仅用于类型定义。并且无论是在数组内部还是外部都没有区别。

此外,您的数组在语法上都不正确。前四个在终止] 之前缺少|,最后一个都缺少。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-25
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 1970-01-01
    相关资源
    最近更新 更多