-
Notifications
You must be signed in to change notification settings - Fork 1k
FAQ: minor improvements to functions vs methods #3068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,18 +244,19 @@ differ from a function value such as: | |
|
||
val square: Int => Int = x => x * x | ||
|
||
For Scala 2, there is a [complete answer on Stack Overflow](https://stackoverflow.com/a/2530007/4111404) | ||
For **Scala 2**, there is a [complete answer on Stack Overflow](https://stackoverflow.com/a/2530007/4111404) | ||
and a [summary with practical differences](https://tpolecat.github.io/2014/06/09/methods-functions.html). | ||
|
||
Note that in **Scala 3** the differences are fewer; | ||
for example, they will be able to | ||
[accept implicit parameters]({{ site.scala3ref }}/contextual/context-functions.html) | ||
as well as [type parameters]({{ site.scala3ref }}/new-types/polymorphic-function-types.html). | ||
In **Scala 3**, the differences are fewer. | ||
[Context functions]({{ site.scala3ref }}/contextual/context-functions.html) | ||
accept given parameters and | ||
[polymorphic functions]({{ site.scala3ref }}/new-types/polymorphic-function-types.html) | ||
have type parameters. | ||
|
||
Nevertheless, it is still recommended to use methods most of the time, | ||
unless you absolutely need a function. And, thanks to | ||
[eta-expansion](https://stackoverflow.com/questions/39445018/what-is-the-eta-expansion-in-scala) | ||
you rarely would need to define a function rather than a method. | ||
Most code uses methods most of the time, | ||
unless a function value is actually needed. With | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In retrospect, we should have added an example, something like:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, good point, I can try to add something like that |
||
[eta-expansion](https://stackoverflow.com/questions/39445018/what-is-the-eta-expansion-in-scala), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if "With" is the best connector to use. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually hesitated over that; you're right to call it out. Thanks for the feedback; I'll ponder some further edits. |
||
a method can be converted as a function when needed. | ||
|
||
### What's the difference between types and classes? | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO we should not only note that most code uses methods but also encourage them as the default, given the syntax, performance, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I see what you mean, but my intent was actually to make the wording stronger, not to weaken it. I think the word "recommend" is sort of weak. Whereas I really want to say quite forcefully, methods are definitely the norm in this language.
Maybe I can find a different wording.