【发布时间】:2011-06-09 08:10:36
【问题描述】:
对于一个类型
type Cow() =
class
member this.Walk () = Console.WriteLine("The cow walks.")
end
我可以编写一个方法来强制方法 Walk 的成员约束
let inline walk_the_creature creature =
(^a : (member Walk : unit -> unit) creature)
// and then do
walk_the_creature (Cow())
在这种情况下,类型是推断出来的。我无法像这样显式地写一个对生物参数的约束
// Does not compile
// Lookup on object of indeterminate type based on information prior to this
// program point. A type annotation may be needed...
let inline walk_the_creature_2 (creature:^a when ^a:(member Walk : unit -> unit)) =
creature.Walk()
我做错了什么?
【问题讨论】: