在F#项目,我有以下几种类型:F#使用在通用功能的约束时类型约束不匹配
type A = { Name: string }
type B = { Name: string; SurName: string }
type C = { Name: string; SurName: string; MaidenName: string }
,并使用在一般的参数的约束以下功能:
let inline filterByName<'a when 'a: (member Name: string)> (name: string) (collection: 'a seq) =
collection |> Seq.where(fun i -> i.Name = name)
问题是,我得到以下编译时错误:
Type constraint mismatch. The type
'a
is not compatible with type
C
The type ''a' does not match the type 'C'
删除inline
从函数的定义给了我下面的编译时错误:
This code is not sufficiently generic. the type variable ^a when ^a:(member get_Name: ^a -> string) could not be generalized because it would escape its scope.
我想实现的是有一个功能,需要一个泛型类型有特定名称的属性,“姓名”,在这种情况下。我在做什么错,或者我错过了什么?
真棒,谢谢老兄 –