How to make a record implement an interface?

浪子不回头ぞ 提交于 2019-12-12 12:22:48

问题


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 ?


回答1:


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";}


来源:https://stackoverflow.com/questions/48606250/how-to-make-a-record-implement-an-interface

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!