-
Notifications
You must be signed in to change notification settings - Fork 1k
update parameterlists #1652
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
Merged
Merged
update parameterlists #1652
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,21 +36,37 @@ println(res) // 55 | |
|
||
Suggested use cases for multiple parameter lists include: | ||
|
||
#### Single functional parameter | ||
#### Drive type inference | ||
|
||
In case of a single functional parameter, like `op` in the case of `foldLeft` above, multiple parameter lists allow a concise syntax to pass an anonymous function to the method. Without multiple parameter lists, the code would look like this: | ||
It so happens that in Scala, type inference proceeds one parameter list at at time. | ||
Say, you have the following method: | ||
martijnhoekstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```tut | ||
def foldleft1[A, B](as: List[A], b0: B, op: (B, A) => B) = ??? | ||
``` | ||
martijnhoekstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
numbers.foldLeft(0, (m: Int, n: Int) => m + n) | ||
|
||
Then you'd like to call it in the following way, but will find that it doesn't compile: | ||
|
||
```tut:fail | ||
def notpossible = foldleft1(numbers, 0, _ + _) | ||
``` | ||
|
||
Note that the use of multiple parameter lists here also allows us to take advantage of Scala type inference to make the code more concise, like this: | ||
you will have to call it like one of the below ways: | ||
|
||
```tut | ||
def firstWay = foldleft1[Int, Int](numbers, 0, _ + _) | ||
def secondWay = foldleft1(numbers, 0, (a: Int, b: Int) => a + b) | ||
``` | ||
numbers.foldLeft(0)(_ + _) | ||
|
||
That's because scala won't be able to infer the type of the function `_ + _`, as it's still inferring `A` and `B`. By moving the parameter `op` to its own parameter list, `A` and `B` are inferred in the first parameter list. These inferred types will then be available to the second parameter list and `_ + _` will match the the inferred type `(Int, Int) => Int` | ||
martijnhoekstra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```tut | ||
def foldleft2[A, B](as: List[A], b0: B)(op: (B, A) => B) = ??? | ||
def possible = foldleft2(numbers, 0)(_ + _) | ||
``` | ||
|
||
this would not be possible with only a single parameter list, as the Scala compiler would not be able to infer the parameter types of the function. | ||
This definition doesn't need any type hints and can infer all of its parameters. | ||
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. s/parameters/type parameters/ |
||
|
||
|
||
#### Implicit parameters | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.