【问题标题】:extending an object with an interface through an object expression通过对象表达式扩展具有接口的对象
【发布时间】:2014-07-08 21:25:52
【问题描述】:

是否可以使用对象表达式在 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" }

【问题讨论】:

    标签: f#


    【解决方案1】:

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

    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
    

    【讨论】:

    • 谢谢你,回答了这个问题,虽然这不是我希望的答案。我现在必须重新考虑我的设计。 stackoverflow 是提出问题的地方,还是我应该去其他地方?
    • 链接到我的后续 uop 问题:stackoverflow.com/questions/24645007/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 2016-07-14
    相关资源
    最近更新 更多