Why and when do I need to follow a method name with _?

耗尽温柔 提交于 2019-12-04 01:12:46

When you omit all parameters in a partially applied function expression, then you need to follow it with _ unless the compiler requires a function type in the place where you use it.

When you call the method :: on Foo, the compiler expects a type Int => Int for the parameter. So you can safely omit the underscore after square in that position.

However, the :: method on Nil can take a parameter of any type. So rather than assume that you meant to partially apply the function, it complains unless you make it absolutely explicit by adding _.

So those are the rules... I can't really enlighten you about why those are the rules; maybe somebody else who has better knowledge of the compiler, the type system, and the language design will be able to tell you why. But I assume that without these rules, there would be a danger of accidental ambiguity in many places.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!