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-19
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
enumWord
50
50
caseGet, North, Go, Pick, Up
51
51
caseItem(name: String)
52
-
52
+
53
53
caseclassCommand(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
importCommand.*
67
-
67
+
68
68
defloop(cmd: Command):Unit=
69
69
cmd match
70
70
caseCommand(North::Nil) =>// Code for going north
@@ -107,7 +107,7 @@ def loop(cmd: Cmd): Unit =
107
107
caseCommand(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
enumFoo:
148
148
caseBar(x: Int)
149
149
caseBaz(y: Int)
150
-
150
+
151
151
deffun=thismatch
152
152
caseBar(z) |Baz(z) => ... // z: Int
153
153
~~~
@@ -161,11 +161,11 @@ Removing the restriction would also allow recursive alternative patterns:
161
161
enumFoo:
162
162
caseBar(x: Int)
163
163
caseBaz(x: Int)
164
-
164
+
165
165
enumQux:
166
166
caseQuux(y: Int)
167
167
caseCorge(x: Foo)
168
-
168
+
169
169
deffun=thismatch
170
170
caseQuux(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
enumFoo:
178
178
caseBar()
179
179
caseBaz(bar: Bar)
180
-
181
-
deffun=thismatch
180
+
181
+
deffun=thismatch
182
182
caseBaz(x) | x @Bar() => ... // x: Foo.Bar
183
183
~~~
184
184
@@ -191,7 +191,7 @@ inferred within within each branch.
191
191
enumFoo:
192
192
caseBar(x: Int)
193
193
caseBaz(y: String)
194
-
194
+
195
195
deffun=thismatch
196
196
caseBar(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
enumFoo[A]:
204
204
caseBar(a: A)
205
205
caseBaz(i: Int) extendsFoo[Int]
206
-
206
+
207
207
deffun=thismatch
208
-
caseBaz(x) |Bar(x) =>// x: Int | A
208
+
caseBaz(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
caseclassContext()
219
-
219
+
220
220
defrun(usingctx: Context):Unit=???
221
-
221
+
222
222
enumFoo:
223
223
caseBar(ctx: Context)
224
224
caseBaz(i: Int, ctx: Context)
225
-
225
+
226
226
deffun=thismatch
227
227
caseBar(givenContext) |Baz(_, givenContext) => 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
enumFoo:
234
234
caseBar(s: String)
235
235
caseBaz(i: Int)
236
-
236
+
237
237
deffun=thismatch
238
238
caseBar(x @givenString) |Baz(x @givenInt) =>???
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
enumFoo[A]:
261
261
caseBar[A](a: A)
262
262
caseBaz[A](a: A)
263
-
263
+
264
264
deftest=thismatch
265
265
caseBar(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-4
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, evenin 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
ThisSIP 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 ofpure aliases. i.e. whenever a series ofpure 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