gpt4 book ai didi

f# - 通过对象表达式扩展具有接口(interface)的对象

转载 作者:行者123 更新时间:2023-12-04 20:06:39 24 4
gpt4 key购买 nike

是否可以使用对象表达式在 F# 中使用接口(interface)装饰对象。例如。:

type IFoo = abstract member foo : string
type IBar = abstract member bar : string
let a = { new IFoo with member x.foo = "foo" }

/// Looking for a variation on the below that does compile, the below doesn't
let b = { a with
interface IBar with
member x.Bar = "bar" }

最佳答案

您不能在运行时使用接口(interface)扩展对象,但可以用另一个对象包装它:

let makeB (a: IFoo) = 
{
new IFoo with
member x.foo = a.foo
interface IBar with
member x.bar = "bar"
}


let a = { new IFoo with member x.foo = "foo" }
let b = makeB a

关于f# - 通过对象表达式扩展具有接口(interface)的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24641886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com