是否可以使用对象表达式使用接口装饰F#中的对象.例如:
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" }
Run Code Online (Sandbox Code Playgroud)
您不能在运行时使用接口扩展对象,但可以用另一个对象包装它:
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
Run Code Online (Sandbox Code Playgroud)