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

Guard Limitations in let #317

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions language/Pattern-Matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,36 @@ compare _ _ = EQ

(The name `otherwise` is a synonym for `true` commonly used in guards.)

Guard Limitations in `let`
----------------------------

Guards are incompatable with Constructor Patterns in `let`. For example, the following function using a `Tuple` constructor pattern will not compile:
```purs
-- This doesn't work
f1 :: Int
f1 =
let
(Tuple a b)
| false = Tuple 1 2
| otherwise = Tuple 3 4
in
a
```
Copy link
Member

@thomashoneyman thomashoneyman Aug 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the capitalization is a bit odd -- we don't usually capitalize "guard" or "constructor". What about:

Guards are not supported with patterns other than simple identifiers in let expressions. For example, this does not compile:

I don't know about linking to purescript/purescript#3200 here, but it's at least accurate to say that this isn't supported right now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than this I think it's sensible to merge this while the restriction does exist.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied your suggestion and also added that link. I'm a fan of linking to relevant issues in the docs.


A workaround is to separate the constructor pattern from the Guard:
milesfrain marked this conversation as resolved.
Show resolved Hide resolved
```purs
f2 :: Int
f2 =
let
t
| false = Tuple 1 2
| otherwise = Tuple 3 4

Tuple a b = t
in
a
```

Pattern Guards
--------------

Expand Down