开发者

Overriding ToString when creating interface type using object expression

Is there a way to override System.Object's virtual methods, particularly To开发者_开发知识库String, when creating an interface type using an object expression?

type INamedObject =
    abstract Name : string

let makeNamedObject name = 
    { new INamedObject with
        member x.Name = name
        override x.ToString() = x.Name } //would like to do this, but doesn't work


It's possible to supplly multiple types to implement / override in an object expression including concrete types. This allows you to both specify Object and INamedObject in the expression and get the desired effect.

let makeNamedObject name =  
    { 
        new System.Object() with 
            member x.ToString() = name
        interface INamedObject with
            member x.Name = name  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜