也许它已经在F#中实现了?
基本上我想用中缀运算符定义一类泛型过滤函数,所以看起来像
type Filter<'T> = ('T -> bool) with
static member (|*) (f:Filter<'T>) (g:Filter<'T>) = (fun x -> (f x) ||
(g x)) // OR operator
但这似乎不是正确的语法
由于错误System.Exception而停止:操作无法执行 由于早期错误而完成类型缩写不能有 2,5类缩写的增量不能有成员3,19
感谢
答案 0 :(得分:4)
你所定义的是type abbreviation,正如错误所表明的那样,既没有增强也没有成员。您可以使用single case discriminated union:
来解决这个问题type Filter<'T> = Filter of ('T -> bool) with
static member (|* ) (Filter f, Filter g) =
Filter(fun x -> f x || g x) // OR operator
当然,您现在需要在布尔操作之前解开谓词函数,然后再次包装组合函数。一个简单的测试...
let odd x = x % 2 <> 0
let big x = x > 10
let (Filter f) = Filter odd |* Filter big in [8..12] |> List.filter f
// val it : int list = [9; 11; 12]