问题描述:

My understanding is that F# records are non-sealed classes. if so, can I inherit a record type? For example:

type person = {name:string; address:string}

type employee inherit person = {employeeId: string}

I have searching around the MSDN docs and language specs and I am not having any luck.

Thanks in advance

网友答案:

F# records cannot be inherited - as Matthew mentions, they are compiled to sealed classes, but it is also an aspect of the F# types system which simply does not allow this.

In practice, you could go with an ordinary class declaration. This means that you won't be able to use the { person with ... } syntax and you won't get the automatic structural equality, but it might make sense if you want to create C#-friendly code:

type Person(name:string) =
  member x.Name = name

type Employee(name:string, id:int) = 
  inherit Person(name)
  member x.ID = id

I think the preferred option would be to use composition rather than inheritance and make employee a record that is composed of some personal information and ID:

type PersonalInformation = { Name : string }

type Employee = 
  { Person : PersonalInformation 
    ID : int }

I probably would not make a person a part of employee (that does not feel right to me, but that is just an intuition), which is why I renamed it to PersonalInformation here.

I suppose another option would be to have IPerson as an interface and have a record Employee implementing the interface:

type IPerson = 
  abstract Name : string

type Employee = 
  { ID : int
    Name : string }
  interface IPerson with
    member x.Name = x.Name

Which one is the best really depends on the concrete thing that you're modelling. But I think interfaces and composition are generally preferred in F# :-)

网友答案:

They are sealed classes, here's the first few lines of the class that's generated for person:

[CompilationMapping(SourceConstructFlags.RecordType)]
[Serializable]
public sealed class person 
: IEquatable<person>, 
  IStructuralEquatable, 
  IComparable<person>, 
  IComparable, 
  IStructuralComparable
相关阅读:
Top