Skip to content

Commit c6d2d86

Browse files
som-snyttjvican
authored andcommitted
Mini-SIP for case if (#1076)
1 parent 06dd528 commit c6d2d86

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

_sips/sips/2018-05-26-case-if.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
layout: sips
3+
discourse: true
4+
title: SIP-NN - Uncluttering Abuse of Match
5+
6+
vote-status: new
7+
vote-text:
8+
permalink: /sips/:title.html
9+
redirect_from: /sips/pending/case-if.html
10+
11+
---
12+
13+
**By: Som Snytt and A. P. Marki**
14+
15+
## History
16+
17+
| Date | Version |
18+
|---------------|---------------|
19+
| May 26th 2018 | Initial Draft |
20+
21+
## Motivation
22+
23+
Since the demise of SIP-12, we have relied on Marie Kondo to declutter
24+
what remains of our lives. But we can do better.
25+
26+
Currently, `case` syntax requires an underscore to represent a pattern,
27+
even if the code of interest is the guard that follows.
28+
29+
Anxiety over this underscore is expressed in [a StackOverflow question][1]
30+
in which a programmer mulls the question of "Abuse of Match" and whether
31+
it actually makes you go blind.
32+
33+
We propose to go underscoreless, pace the famous consultancy.
34+
35+
If we can't have SIP-12 then we can have tidy syntax for `if-then` in `case` blocks.
36+
37+
## Syntax
38+
39+
In lieu of
40+
41+
42 match {
42+
case _ if now isAfter midnight => nothingGoodHappens()
43+
}
44+
45+
we write
46+
47+
42 match {
48+
case if now isAfter midnight => nothingGoodHappens()
49+
}
50+
51+
The syntax accepts either a pattern with optional guard, or a guard with no pattern:
52+
53+
CaseClause ::= ‘case’ (Pattern [Guard] | Guard) ‘=>’ Block
54+
Guard ::= ‘if’ PostfixExpr
55+
56+
A guard with no pattern is taken as though the pattern were an underscore, which matches
57+
any value.
58+
59+
This modest proposal eschews dispensing with the pattern altogether, `case =>`.
60+
61+
## Further Justifications
62+
63+
In a respected online [forum][2] which brooks no fools, [Mulliganaceous][3] has posted
64+
an "accepted" answer using the idiom, "in case if". This supports `case if` as
65+
a natural locution.
66+
67+
In a response to one judgment about abuse of match, [one Scala user][4] whose handle I can
68+
never spell quite right let alone pronounce finds the match version
69+
"clearer and visually more pleasant" than the cluttered `if` expression.
70+
71+
From the beginning, Scala has made great strides in reducing vertical space in source code.
72+
However, we are still constrained horizontally, despite curved OLED screens.
73+
Recently, [a suggested edit][5] was declined because of maximum line length restrictions.
74+
Every wasted character brings us closer to an unfortunate line break.
75+
76+
## Implementation
77+
78+
An implementation is [available][6]. It's pretty slick.
79+
80+
## References
81+
82+
1. [Abuse of Match?][1]
83+
2. [Reputation requirements for creating tags and tag synonyms][2]
84+
3. [Mulliganaceous user profile][3]
85+
4. [huynhjl user profile][4]
86+
5. [Sample line length limitation in a Scala project][5]
87+
6. [Implementation][6]
88+
89+
[1]: https://stackoverflow.com/questions/12556236/abuse-of-match "Abuse of Match?"
90+
[2]: https://meta.stackoverflow.com/a/368537/1296806 "Reputation requirements for creating tags and tag synonyms"
91+
[3]: https://meta.stackoverflow.com/users/8242447/mulliganaceous "Mulliganaceous"
92+
[4]: https://stackoverflow.com/users/257449/huynhjl "huynhjl"
93+
[5]: https://github.com/apache/spark/pull/21369/files#r189794046 "scala-style enforces a max of 100 chars per line"
94+
[6]: https://github.com/scala/scala/pull/6241 "Implementation PR 6241"
95+

0 commit comments

Comments
 (0)