Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions errors/IncorrectAnonymousArgument.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,54 @@
```purescript
module Example where

add = (_ + _)
Copy link
Contributor Author

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:

  • Not ideal to overwrite Prelude functions, while also using the corresponding infix operator.
  • Even with a different name, it's trivially replaced with add or (+).
  • Unclear whether the intention in the broken case is to reuse the same anonymous arg, or consider these separate args. And if separate args, is the expectation that they are populated in order?

sub3 = _ - 3

mapArray = map _ [1, 2, 3]
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 map examples to use first-order arguments for _.

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
Expand Down