用于实现 'virtual' 属性(实现接口(interface))的 F# 语法

标签 f# c#-to-f#

我需要实现IDataErrorInfo基类中的接口(interface)。该接口(interface)需要一个属性和一个索引器。我想为两者提供默认实现并允许子类覆盖它。我似乎无法使用接口(interface)实现的语法来使用“虚拟”实现的语法!例如:

type ViewModelBase() =
  interface IDataErrorInfo with
    abstract Error : string with get
    default this.Error with get() = ""

出现以下编译错误

Error 1 Unexpected keyword 'abstract' in member definition. Expected 'member', 'override' or other token. D:\MinorApps\VetCompass\VetCompass\ViewModel\ViewModelBase.fs 18 7 VetCompass

Error 2 Incomplete structured construct at or before this point in pattern D:\MinorApps\VetCompass\VetCompass\ViewModel\ViewModelBase.fs 19 7 VetCompass

我什至不知道从哪里开始使用索引器!

最佳答案

所有接口(interface)实现都是显式的,这意味着当被视为类的成员时,接口(interface)的方法将是私有(private)的。因此,您不能在实现中使用 abstractdefault 修饰符。相反,您需要添加一些重复内容:

type ViewModelBase() =
    // declare a new virtual property
    abstract Error : string
    default this.Error = ""

    interface IDataErrorInfo with
       // pass through to the virtual property implementation
       member this.Error = this.Error

关于用于实现 'virtual' 属性(实现接口(interface))的 F# 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12860712/

相关文章:

f# - 使用Mono Debug扩展在VSCode中调试F#控制台应用程序时不会出现断点

f# - 将 c# 委托(delegate)与 f# 函数一起使用

f# - 使用更新的片段定义大型不可变类型的正确方法?

f# - 如何等待异步方法,类似于 C#

f# - 一次声明静态和实例成员的惯用方法?

f# - 需要有关将 C# 转换为 F# 的帮助

F# 输出参数和值类型

azure - F# Azure Function 的 Cosmos DB 绑定(bind)无法绑定(bind)

f# - 使用强制 F# 创建具有自定义类型的 "plus"函数

events - 应该如何在 F# 接口(interface)中声明事件?