Is it possible to decorate an object in F# with an interface using an object expression. E.g.:
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" }
A Java Interface does not extend the java. lang. Object class but instances of objects that implement the interface extend the Object class otherwise if Java Interfaces allowed to extend the java.
An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.
Interfaces can be extended like classes using the extends operator. Note: The class implementing the interface must declare all methods in the interface with a compatible signature.
An interface is a contract without implementations (Java8 introduced default methods). By extending you extend the contract with new "names" to be implemented by concrete class.
You can't extend an object with an interface at run-time, but you could wrap it with another object:
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
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