Is there a way to override System.Object's virtual methods, particularly ToString, 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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With