f# - 有没有办法让记录继承 F# 中的抽象类?

标签 f#

假设我有一个接口(interface) IEvent .这个接口(interface)有一个 Id我在 C# 中使用以下实现的属性:

public abstract class EventBase : IEvent
{
    public Guid Id { get; protected set; }
]

有没有办法在 F# 记录中继承这个抽象类?

就像是:
type OneOfMyEvents = {Id: Guid, Prop: string}
    inherit EventBase(Id)

问题是我想将我的所有事件定义为记录,因为它真的很方便,但我也想强制我的所有事件都具有我不必每次都输入的 Id 属性。

最佳答案

记录不能从类继承,但它们可以实现接口(interface)。例如,使用您的 IEvent界面:

open System

[<Interface>]
type IEvent =
    abstract Id : Guid with get


type MyRecord = {
    Id: Guid;
    Prop: string;
} with
    interface IEvent with
        member this.Id
            with get () = this.Id

关于f# - 有没有办法让记录继承 F# 中的抽象类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22517957/

相关文章:

f# - f# 中内置 "pairify"函数?

f# - 定义可区分联合的默认值

f# - 使用 FParsec 我将如何解析 : line ending in newline <|> a line ending with eof

events - F# 中的松散耦合代理

recursion - 为什么这个 F# 内部函数不是尾递归的?

f# - 类似于 C++ 引用访问?

http - F# HTTP Post,读取状态码响应

f# - 如何解析重复模式之后正确的时间之间的关系?

.net - 如何将 F#'s "与“赋值关键字与记录中的列表属性一起使用?”

F# 初学者在多个依赖名称上使用 let