Changes between Version 3 and Version 4 of DoAndIfThenElse
- Timestamp:
- 12/17/05 03:28:08 (7 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
DoAndIfThenElse
v3 v4 1 = The `do`-notation and `if`-`then`-`else`=1 = Conditionals and `do`-notation = 2 2 [[PageOutline]] 3 4 This page concerns the possibility of tweaking the layout rules to avoid a common pitfall when `if`-`then`-`else` is used with the `do`-notation.5 It only documents a problem: no specific solution is proposed. The issue is somewhat related to NondecreasingIndentation.6 Strictly speaking, the issue is not tied to the `do`-notation, but that is where it (almost?) always shows up.7 3 8 4 == Brief Explanation == … … 22 18 put beginners off. 23 19 24 Might it be possible to handle `then` and `else` specially to avoid this problem? At least in the context of `do`? 25 Or might it be possible to treat `if` as something that opens a kind of explicit layout context (which would 26 prevent the insertion of a closing `}`) that is closed by a matching `else`? 20 Strictly speaking, the issue is not tied to the `do`-notation, but that is where it (almost?) always shows up. 27 21 28 == Implementation==22 == Proposal == 29 23 30 This has been implemented in jhc by changing a single line in the parser, an equivalant change in the report should have the same effect. 24 Change the syntax for conditionals to 31 25 32 {{{ 33 old: 34 | 'if' exp 'then' exp 'else' exp { HsIf $2 $4 $6 } 35 new: 36 | 'if' exp optsemi 'then' exp optsemi 'else' exp { HsIf $2 $5 $8 } 37 }}} 26 exp -> `if` exp,,1,, [`;`] `then` exp,,2,, [`;`] `else` exp,,3,, 38 27 28 i.e., add optional semicolons before `then` and `else`, making the above example legal. 29 This has been recently added to jhc and GHC, and 39 30 so far, it has not caused any problems. 40 31 41 32 == References == 33 * [http://www.haskell.org/onlinereport/exps.html#conditionals Conditionals] in the Haskell 98 Report 42 34 * Somewhat related to NondecreasingIndentation 43 35 44 36 == Pros == 45 * Would address a layout issue that manifestly trips up a lot of people and which at least one 46 experienced Haskell programmer (me!) finds very annoying. 37 * Would address a layout issue that manifestly trips up a lot of people, and many experienced Haskell programmers find very annoying. 38 * Trivial to implement 39 * Independent of the layout rule. 47 40 48 41 == Cons == 49 * No concrete proposal yet50 * Not implemented in any system to my knowledge
