Skip to content

Commit 02862ea

Browse files
committed
Document GHC-01239 (closes #288)
1 parent 76c909a commit 02862ea

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+
example :: Int
10+
example = length (if True then [1] else [])
11+
12+
-- Works without BlockArguments
13+
example2 :: Int
14+
example2 = 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 argument we either have to surround it in parentheses,
6+
use function application operator `($)` or enable [`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: TODO
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+
```language-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+
Thinking of if-then-else expression as a value, we might want to pass it as an input to a function.
19+
If you do that you can run into the following error.

0 commit comments

Comments
 (0)