You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _sips/sips/alternative-bind-variables.md
+19-19Lines changed: 19 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -49,7 +49,7 @@ Typically, the commands are tokenized and parsed. After a parsing stage we may e
49
49
enum Word
50
50
case Get, North, Go, Pick, Up
51
51
case Item(name: String)
52
-
52
+
53
53
case class Command(words: List[Word])
54
54
```
55
55
@@ -64,7 +64,7 @@ matching on a single stable identifier, `North` and the code would look like thi
64
64
65
65
~~~ scala
66
66
import Command.*
67
-
67
+
68
68
def loop(cmd: Command): Unit =
69
69
cmd match
70
70
case Command(North :: Nil) => // Code for going north
@@ -107,7 +107,7 @@ def loop(cmd: Cmd): Unit =
107
107
case Command(Get :: Item(name)) => pickUp(name)
108
108
~~~
109
109
110
-
Or any number of different encodings. However, all of them are less intuitive and less obvious than the code we tried to write.
110
+
Or any number of different encodings. However, all of them are less intuitive and less obvious than the code we tried to write.
111
111
112
112
## Commentary
113
113
@@ -147,7 +147,7 @@ type, like so:
147
147
enum Foo:
148
148
case Bar(x: Int)
149
149
case Baz(y: Int)
150
-
150
+
151
151
def fun = this match
152
152
case Bar(z) | Baz(z) => ... // z: Int
153
153
~~~
@@ -161,11 +161,11 @@ Removing the restriction would also allow recursive alternative patterns:
161
161
enum Foo:
162
162
case Bar(x: Int)
163
163
case Baz(x: Int)
164
-
164
+
165
165
enum Qux:
166
166
case Quux(y: Int)
167
167
case Corge(x: Foo)
168
-
168
+
169
169
def fun = this match
170
170
case Quux(z) | Corge(Bar(z) | Baz(z)) => ... // z: Int
171
171
~~~
@@ -177,8 +177,8 @@ We also expect to be able to use an explicit binding using an `@` like this:
177
177
enum Foo:
178
178
case Bar()
179
179
case Baz(bar: Bar)
180
-
181
-
def fun = this match
180
+
181
+
def fun = this match
182
182
case Baz(x) | x @ Bar() => ... // x: Foo.Bar
183
183
~~~
184
184
@@ -191,7 +191,7 @@ inferred within within each branch.
191
191
enum Foo:
192
192
case Bar(x: Int)
193
193
case Baz(y: String)
194
-
194
+
195
195
def fun = this match
196
196
case Bar(x) | Baz(x) => // x: Int | String
197
197
~~~
@@ -203,26 +203,26 @@ the following case to match all instances of `Bar`, regardless of the type of `A
203
203
enum Foo[A]:
204
204
case Bar(a: A)
205
205
case Baz(i: Int) extends Foo[Int]
206
-
206
+
207
207
def fun = this match
208
-
case Baz(x) | Bar(x) => // x: Int | A
208
+
case Baz(x) | Bar(x) => // x: Int | A
209
209
~~~
210
210
211
211
### Given bind variables
212
212
213
-
It is possible to introduce bindings to the contextual scope within a pattern match branch.
213
+
It is possible to introduce bindings to the contextual scope within a pattern match branch.
214
214
215
215
Since most bindings will be anonymous but be referred to within the branches, we expect the _types_ present in the contextual scope for each branch to be the same rather than the _names_.
216
216
217
217
~~~ scala
218
218
case class Context()
219
-
219
+
220
220
def run(using ctx: Context): Unit = ???
221
-
221
+
222
222
enum Foo:
223
223
case Bar(ctx: Context)
224
224
case Baz(i: Int, ctx: Context)
225
-
225
+
226
226
def fun = this match
227
227
case Bar(given Context) | Baz(_, given Context) => run // `Context` appears in both branches
228
228
~~~
@@ -233,7 +233,7 @@ This begs the question of what to do in the case of an explicit `@` binding wher
233
233
enum Foo:
234
234
case Bar(s: String)
235
235
case Baz(i: Int)
236
-
236
+
237
237
def fun = this match
238
238
case Bar(x @ given String) | Baz(x @ given Int) => ???
239
239
~~~
@@ -254,13 +254,13 @@ However, since untagged unions are part of Scala 3 and the fact that both are re
254
254
255
255
#### Type ascriptions in alternative branches
256
256
257
-
Another suggestion is that an _explicit_ type ascription by a user ought to be defined for all branches. For example, in the currently proposed rules, the following code would infer the return type to be `Int | A` even though the user has written the statement `id: Int`.
257
+
Another suggestion is that an _explicit_ type ascription by a user ought to be defined for all branches. For example, in the currently proposed rules, the following code would infer the return type to be `Int | A` even though the user has written the statement `id: Int`.
258
258
259
259
~~~scala
260
260
enum Foo[A]:
261
261
case Bar[A](a: A)
262
262
case Baz[A](a: A)
263
-
263
+
264
264
def test = this match
265
265
case Bar(id: Int) | Baz(id) => id
266
266
~~~
@@ -295,7 +295,7 @@ If `p_i` is a quoted pattern binding a variable or type variable, the alternativ
295
295
296
296
Each $`p_n`$ must introduce the same set of bindings, i.e. for each $`n`$, $`\Gamma_n`$ must have the same **named** members $`\Gamma_{n+1}`$ and the set of $`{T_0, ... T_n}`$ must be the same.
297
297
298
-
If $`X_{n,i}`$, is the type of the binding $`x_i`$ within an alternative $`p_n`$, then the consequent type, $`X_i`$, of the
298
+
If $`X_{n,i}`$, is the type of the binding $`x_i`$ within an alternative $`p_n`$, then the consequent type, $`X_i`$, of the
299
299
variable $`x_i`$ within the pattern scope, $`\Gamma`$ is the least upper-bound of all the types $`X_{n, i}`$ associated with
Copy file name to clipboardExpand all lines: _sips/sips/better-fors.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -54,7 +54,7 @@ There are some clear pain points related to Scala'3 `for`-comprehensions and tho
54
54
55
55
This complicates the code, even in this simple example.
56
56
2. The simplicity of desugared code
57
-
57
+
58
58
The second pain point is that the desugared code of `for`-comprehensions can often be surprisingly complicated.
59
59
60
60
e.g.
@@ -92,7 +92,7 @@ There are some clear pain points related to Scala'3 `for`-comprehensions and tho
92
92
This SIP suggests the following changes to `for` comprehensions:
93
93
94
94
1. Allow `for` comprehensions to start with pure aliases
95
-
95
+
96
96
e.g.
97
97
```scala
98
98
for
@@ -103,7 +103,7 @@ This SIP suggests the following changes to `for` comprehensions:
103
103
```
104
104
2. Simpler conditional desugaring of pure aliases. i.e. whenever a series of pure aliases is not immediately followed by an `if`, use a simpler way of desugaring.
105
105
106
-
e.g.
106
+
e.g.
107
107
```scala
108
108
for
109
109
a <- doSth(arg)
@@ -250,7 +250,7 @@ A new desugaring rules will be introduced for simple desugaring.
0 commit comments