Skip to content

Commit 8d9be5b

Browse files
Merge pull request #391 from jhrcek/GHC-01239
Document GHC-01239 (closes #288)
2 parents 76c909a + 92478b5 commit 8d9be5b

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{-# LANGUAGE BlockArguments #-}
2+
module IfInFunAppExpr where
3+
4+
-- Requires BlockArguments language extension to work
5+
example :: Int
6+
example = length if True then [1] else []
7+
8+
-- Works without BlockArguments
9+
example2 :: Int
10+
example2 = length (if True then [1] else [])
11+
12+
-- Works without BlockArguments
13+
example3 :: Int
14+
example3 = length $ if True then [1] else []
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module IfInFunAppExpr where
2+
3+
example :: Int
4+
example = length if True then [1] else []
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Unexpected if expression in function application
3+
---
4+
5+
To pass If-Then-Else expressions as function arguments we either have to surround them in parentheses,
6+
use the function application operator `($)` or enable the [`BlockArguments`](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/block_arguments.html#extension-BlockArguments) extension.
7+
8+
## Error Message
9+
```
10+
IfInFunAppExpr.hs:4:18: error:
11+
Unexpected if expression in function application:
12+
if True then [1] else []
13+
Suggested fixes:
14+
• Use parentheses.
15+
• Perhaps you intended to use BlockArguments
16+
|
17+
4 | example = length if True then [1] else []
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^
19+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
title: Unexpected if expression in function application
3+
summary: If expression used as function argument
4+
severity: error
5+
introduced: 9.6.1
6+
---
7+
8+
Unlike in many other languages, in Haskell the If-Then-Else construct is an expression, which means it returns a value that can be processed further.
9+
10+
```haskell
11+
ageMessage :: Int -> String
12+
ageMessage age = if age < 18 then "You are too young to enter" else "Welcome to the club"
13+
14+
putStrLn (ageMessage 10) -- You are too young to enter
15+
putStrLn (ageMessage 20) -- Welcome to the club
16+
```
17+
18+
Because If-Then-Else expressions return values, it makes sense to pass them as input to a function.
19+
However, without language extensions, Haskell's grammar requires parentheses around them in function argument positions.

0 commit comments

Comments
 (0)