2016-02-03 82 views
5

在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.

我想实现的是有一个功能,需要一个泛型类型有特定名称的属性,“姓名”,在这种情况下。我在做什么错,或者我错过了什么?

回答

5

问题在于如何调用受限制的成员 - 不能使用i.Name语法,而是必须使用更详细的语法。在光明的一面,这使该方法本身的签名推断,所以你不必重复一切:

let inline filterByName name collection = 
    collection |> Seq.where(fun i -> (^a : (member Name : string) i) = name) 

还要注意的是,你需要使用一个静态解析类型的变量(^a)代替的正常泛型类型变量('a)。

+0

真棒,谢谢老兄 –