Let's say I have the following interface in F#:
type InterfaceA =
abstract Magic : InterfaceA -> InterfaceA
How can I implement such interface? When I try to do it like this:
type MyTypeA = {x:int} with
interface InterfaceA with
member self.Magic another =
{x=self.x+another.x}
I get the error:
This expression was expected to have type 'InterfaceA' but here has type 'MyTypeA'
To fix the type error, you need to explicitly cast the returned value to the
InterfaceAtype - unlike for example C#, F# does not do this automatically:Note that your code also did not work because
anotherwas of typeInterfaceAand so it did not have thexfield you could access. To fix this, I added a memberValueto the interface.