如何在F#中使用可为空的抽象类型?
以下是我试图做的:如何在F#中使用可为空的抽象类型?,f#,nullable,F#,Nullable,以下是我试图做的: and [<AbstractClass>] Figure() = let mutable name : string = "" let mutable color : ColorType = WHITE abstract member Name : string member F.Color with get() = color and set v = color <-
and [<AbstractClass>] Figure() =
let mutable name : string = ""
let mutable color : ColorType = WHITE
abstract member Name : string
member F.Color
with get() = color
and set v = color <- v
abstract member motion : Dashboard -> SPosition -> ColorType -> list<SPosition * CellDashboard * bool>;
override F.ToString() = name
and CellDashboard(addingFigure : Nullable<Figure>) as C =
let mutable attacked : bool = false;
let mutable cleaned : bool = false;
let mutable _CellState : Nullable<Figure> = Nullable<Figure>(addingFigure);
为什么??如何避免此错误?可为空的类型只能用于.NET值类型。错误消息本质上意味着编译器无法检查约束是否有效(因为类型是抽象的)
如果要用F#表示缺少的值,最好使用选项
如果您正在编写将从C#使用的代码,那么最好避免暴露F#特定的类型(如option
),并且可以使用AllowNullLiteral
标记该类型,这使得可以将null
作为类型的值传递(但是null
是邪恶的,因此除非必要,否则不应该这样做!)可为空的类型只能用于.NET值类型。错误消息本质上意味着编译器无法检查约束是否有效(因为类型是抽象的)
如果要用F#表示缺少的值,最好使用选项
如果您正在编写将从C#使用的代码,那么最好避免暴露F#特定的类型(如option
),并且可以使用AllowNullLiteral
标记该类型,这使得可以将null
作为类型的值传递(但是null
是邪恶的,因此除非必要,否则不应该这样做!)
Error 1 A generic construct requires that the type 'Figure' be non-abstract