[英]F#: Function parameter defaults to obj in class
I created a simple function: 我创建了一个简单的函数:
let myGenericFunc abc = printfn "%A" abc
Its type is: 它的类型是:
'a -> unit
Then I want to make it a member of a class: 然后我想让它成为一个类的成员:
type MyClass() =
member x.Func = myGenericFunc
However, type of "Func" is now 但是,现在是“Func”的类型
obj -> unit
Moreover: If I make the parameter explicit, everything is ok again: 而且:如果我将参数设为显式,那么一切都可以了:
type MyClass() =
// Func : 'a -> unit
member x.Func y = myGenericFunc y
The question is: what happens ?! 问题是:会发生什么?!
Your first x.Func
is defining a property, not a method and because properties cannot be generic it has to use a concrete type for 'a
. 您的第一个x.Func
被定义属性,而不是方法,因为性能不能通用它必须用一个具体类型'a
。
When you define x.Func y
you are creating a method and that can be generic. 当你定义x.Func y
你正在创建一个方法,它可以是通用的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.