我是F#的新手。我正在搞乱,我发现了一些有趣的东西,我希望有人能够启发我,看看幕后的情况。为什么函数绑定到它们传递的第一个类型
所以我做了这个功能:let my_func (x, y) = x + y
。
然后我用参数1
和2
给我调用该函数。这是我期望发生的事情,但是当我将两个字符串传递给my_func
时,即使+
是一个有效的字符串操作符,我也遇到了错误。我重新编写了我的代码,但这次只打电话my_func
与"cat"
和" dog"
哪给了我"cat dog"
。然后我试图通过1
和2
返回到my_func
只发现my_func
不接受整数。
为什么my_func
这样表现?
let my_func (x, y) = x + y
my_func (1, 2) // produces => 3
my_func ("cat", " dog") // Error
重新运行程序 ...
let my_func (x, y) = x + y
my_func ("cat", " dog") // produces => "cat dog"
my_func (1, 2) // Error
+1伟大的答案。 Se也[这篇由Liam MCLennnan撰写的文章涵盖了类似的地方](http://withouttheloop.com/articles/2014-10-21-fsharp-adhoc-polymorphism/) – 2014-10-31 08:28:49