简体   繁体   中英

How can I use Nullable abstract types in F#?

Here is what am I trying to do :

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

the trouble is :

Error   1   A generic construct requires that the type 'Figure' be non-abstract

why ? And how can I avoid this error ?

The Nullable<T> type can only be used for .NET value types. The error message essentially means that the compiler cannot check whether the constraint holds or not (because the type is abstract).

If you want to represent missing value in F#, it is better to use option<T> instead.

If you're writing code that will be used from C#, then it is better to avoid exposing F#-specific types (like option<T> ) and you can mark the type with AllowNullLiteral , which makes it possible to pass null as a value of the type (but null is evil, so this shouldn't be done unless necessary!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM