If I have an interface:
type IData =
abstract member firstName: string
abstract member lastName: string
How do I define a record type that complies with this interface.
I tried something like below:
> type Data = { firstName: string; lastName: string } interface IData ;;
Snippet.js(43,63): error FS0366: No implementation was given for 'abstract member IData.firstName : string'. Note that all interface members must be implemented
and listed under an appropriate 'interface' declaration, e.g. 'interface ... with member ...'.
From the official reference for Records:
Record fields differ from classes in that they are automatically exposed as properties
My first question is: If properties are "automatically exposed" then why do I need to "do something" to implement them.
Since the error message askes me to provide an implementation for the interface, I tried the following:
> type Data = { firstName: string; lastName: string; } interface IData with
- member this.firstName with get () = this.firstName
- member this.lastName with get () = this.lastName
type Data =
{firstName: string;
lastName: string;}
with
interface IData
end
So far so good, however now when I try to use this, I run into issues:
> let d: IData = { firstName = "john"; lastName = "doe" } ;;
error FS0001: This expression was expected to have type
'IData'
but here has type
'Data'
Another Attempt:
> let d = { firstName = "john"; lastName = "doe" }
- ;;
val d : Data = {firstName = "john";
lastName = "doe";}
> let d2: IData = d ;;
C:\Users\loref\Workspace\source-nly10r\Untitled-1(25,17): error FS0001: This expression was expected to have type
'IData'
but here has type
'Data'
So, my second question is that if Data
implements IData
then why can't I assign a value of Data
type to a variable of IData
type ?
As pointed by Gustavo, implicit interface implementation is being discussed by F# implementers, and is not currently available.
Wrt. my second question, explicit casting is required:
> let d2: IData = d :> IData ;;
val d2 : IData = {firstName = "john";
lastName = "doe";}
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