我有以下代码:
type CapturablePieces = Pawn | Knight | Bishop | Rook | Queen
我可以定义另一种类型,如下所示:
type Pieces = Pawn | Knight | Bishop | Rook | Queen | King
但是,这里显然有很多代码重复。有没有一种方法可以通过将CapturablePieces合并到Pieces的定义中来避免这种情况?

最佳答案

如果您不需要CapturablePieces是可分离类型,则仅是某些特殊情况的子集,也许可以将其实现为成员?我可以想到两种方式:

a)收集静态成员中的可捕获片段:

type Pieces = Pawn | Knight | Bishop | Rook | Queen | King
    static member Capturable = [Pawn; Knight; Bishop; Rook; Queen]

b) bool 成员:
type Pieces = Pawn | Knight | Bishop | Rook | Queen | King
    member x.IsCapturable = match x with King -> false | _ -> true

09-28 11:13