简体   繁体   中英

Is it possible to declare an abstract auto-implemented property in F#?

I'm trying to do the following F# equivalent:

[C#]
public virtual int Property { get; set; }

But this code (and many other combinations) does not work:

[F#]
abstract member Id: int with get, set
default val this.Id = 123 with get, set

这在Dev11预览中是不可能的。

Ok, looking at the MSDN documentation on F# properties, this line in the abstract properties immediately jumps out:

Alternatively, abstract can just mean that a property is virtual, and in that case, a definition must be present in the same class.

The example syntax they give is:

type Base1() =
   let mutable value = 10
   abstract Property1 : int with get, set
   default this.Property1 with get() = value and set(v : int) = value <- v

Now I'm not overly familiar with F#, but my guess is that you could set let mutable value = 10 to your requirement of let mutable value = 123

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