对于类型
type Cow() =
class
member this.Walk () = Console.WriteLine("The cow walks.")
end
我可以编写一个强制方法约束的方法,如
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()
我做错了什么?
答案 0 :(得分:25)
它没有明确地写出问题的约束,而是语法不太好,你可以在参数上放置成员约束,然后以通常的方式调用成员。 walk_the_creature
和walk_the_creature2
的正文在此处相同:
let inline walk_the_creature_2 (creature:^a when ^a:(member Walk : unit -> unit)) =
(^a : (member Walk : unit -> unit) creature)