【发布时间】:2016-09-06 12:14:14
【问题描述】:
我正在试验内联和运算符....不是特定的应用程序。
基于阅读诸如 http://nut-cracker.azurewebsites.net/blog/2011/11/15/typeclasses-for-fsharp/ 但不理解。
所以。
我可以做
type HelperType = HelperType with
static member (=>) (d:HelperType,x: list<char>) = fun y -> x @ y
static member (=>) (d:HelperType,x:array<char>) = fun y -> Array.append x y
static member (=>) (d:HelperType,x:string ) = fun y -> x + y
let inline append x = HelperType => x
let x1 = append "" ""
let x1 = append [|'a'|] [|'b'|]
工作......并且有点了解发生了什么。
但是让我们尝试重载类型而不是值...所以我去...
type TypeOf<'t> = | TypeOf
type HelperType = HelperType with
static member (<*>) (d:HelperType,x:TypeOf<list<char>> ) = fun x y -> x @ y
static member (<*>) (d:HelperType,x:TypeOf<array<char>> ) = fun x y -> Array.append x y
即我实际上不需要类型的值,只是一些代理类型值
我走了
let inline append2 (x : ^t) = (HelperType <*> (TypeOf :> TypeOf< ^t>)) x
我得到...
Error A unique overload for method 'op_LessMultiplyGreater' could not be determined based on type information prior to this program point. A type annotation may be needed. Candidates: static member HelperType.( <*> ) : d:HelperType * x:TypeOf<char array> -> ('a0 [] -> 'a0 [] -> 'a0 []), static member HelperType.( <*> ) : d:HelperType * x:TypeOf<char list> -> ('a0 list -> 'a0 list -> 'a0 list)
但是第一个工作示例和第二个工作示例之间有什么区别?
【问题讨论】:
标签: f#