-
Notifications
You must be signed in to change notification settings - Fork 299
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
Add more examples for anonymous argument error #369
Open
milesfrain
wants to merge
1
commit into
purescript:master
Choose a base branch
from
milesfrain:patch-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -5,31 +5,54 @@ | |
```purescript | ||
module Example where | ||
|
||
add = (_ + _) | ||
sub3 = _ - 3 | ||
|
||
mapArray = map _ [1, 2, 3] | ||
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 think this second-order example adds another layer of potential confusion for beginners. I changed all these |
||
square = (_ * _) | ||
result1 = map (_ * _) [1, 2, 3] | ||
|
||
result2 = map (div _ 2) [1, 2, 3] | ||
|
||
result3 = map (1 + _ / 2) [1, 2, 3] | ||
``` | ||
|
||
## Cause | ||
|
||
In an [operator section](https://github.com/purescript/documentation/blob/fc4a9db4b128aa3331e5f990cb1860e59077af31/language/Syntax.md#operator-sections), like `(_ + 1)`, an anonymous argument can be used only once. | ||
|
||
An [operator section](https://github.com/purescript/documentation/blob/master/language/Syntax.md#operator-sections), like `(_ + 1)`, has the following requirements: | ||
- The expression must be bracketed (surrounded by parens). | ||
- An anonymous argument can be used only once. | ||
- The anonymous argument must neighbor a [binary operator](https://github.com/purescript/documentation/blob/master/language/Syntax.md#binary-operators). | ||
- The anonymous argument must be the _sole expression_ for one of the binary operator arguments. | ||
|
||
## Fix | ||
|
||
In the case of multiple arguments, give them names: | ||
Surround with parens: | ||
```purescript | ||
add a b = (a + b) | ||
sub3 = (_ - 3) | ||
``` | ||
or in the case of a normal function: Write the function [as an operator](https://github.com/purescript/documentation/blob/fc4a9db4b128aa3331e5f990cb1860e59077af31/language/Syntax.md#functions-as-operators) using backticks: | ||
|
||
Replace multiple anonymous arguments with named arguments: | ||
```purescript | ||
mapArray = _ `map` [1, 2, 3] | ||
square a = a * a | ||
result1 = map (\n -> n * n) [1, 2, 3] | ||
``` | ||
|
||
Write the function [as an operator](https://github.com/purescript/documentation/blob/master/language/Syntax.md#functions-as-operators) using backticks: | ||
```purescript | ||
result2 = map (_ `div` 2) [1, 2, 3] | ||
``` | ||
or simply use the corresponding infix operator if one is available: | ||
```purescript | ||
result2 = map (div _ 2) [1, 2, 3] | ||
``` | ||
|
||
Replace the anonymous argument with a named argument: | ||
```purescript | ||
result3 = map (\n -> 1 + n / 2) [1, 2, 3] | ||
``` | ||
|
||
## Notes | ||
|
||
- While `_ + _` will give this error; `\a -> a + _` will not | ||
- While `(_ + _)` will give this error; `\a -> (a + _)` will not | ||
|
||
- If you really want to have multiple anonymous arguments, it can be achieved like this: | ||
```purescript | ||
|
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.
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.
This
add
example is problematic because:add
or(+)
.