Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a record implement an interface?

Tags:

f#

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 ?

like image 573
lorefnon Avatar asked Feb 04 '18 08:02

lorefnon


1 Answers

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";}
like image 98
lorefnon Avatar answered Oct 05 '22 23:10

lorefnon