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: content/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
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: content/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, 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.
Copy file name to clipboardExpand all lines: content/polymorphic-eta-expansion.md
+13-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ permalink: /sips/:title.html
21
21
- For a first-time reader, a high-level overview of what they should expect to see in the proposal.
22
22
- For returning readers, a quick reminder of what the proposal is about. -->
23
23
24
-
We propose to extend eta-expansion to polymorphic methods.
24
+
We propose to extend eta-expansion to polymorphic methods.
25
25
This means automatically transforming polymorphic methods into corresponding polymorphic functions when required, for example:
26
26
27
27
~~~scala
@@ -44,10 +44,10 @@ This section should clearly express the scope of the proposal. It should make it
44
44
45
45
Regular eta-expansion is so ubiquitous that most users are not aware of it, for them it is intuitive and obvious that methods can be passed where functions are expected.
46
46
47
-
When manipulating polymorphic methods, we wager that most users find it confusing not to be able to do the same.
47
+
When manipulating polymorphic methods, we wager that most users find it confusing not to be able to do the same.
48
48
This is the main motivation of this proposal.
49
49
50
-
It however remains to be demonstrated that such cases appear often enough for time and maintenance to be devoted to fixing it.
50
+
It however remains to be demonstrated that such cases appear often enough for time and maintenance to be devoted to fixing it.
51
51
To this end, the remainder of this section will show a manufactured example with tuples, as well as real-world examples taken from the [Shapeless-3](https://index.scala-lang.org/typelevel/shapeless-3) and [kittens](https://index.scala-lang.org/typelevel/kittens) libraries.
52
52
53
53
@@ -89,7 +89,7 @@ There is however the following case, where a function is very large:
89
89
case (acc, Some(t)) =>Some((t, acc._1))
90
90
}
91
91
}
92
-
~~~
92
+
~~~
93
93
94
94
By factoring out the function, it is possible to make the code more readable:
95
95
@@ -113,7 +113,7 @@ By factoring out the function, it is possible to make the code more readable:
113
113
case (acc, Some(t)) =>Some((t, acc._1))
114
114
}
115
115
}
116
-
~~~
116
+
~~~
117
117
118
118
It is natural at this point to want to transform the function into a method, as the syntax for the latter is more familiar, and more readable:
119
119
@@ -139,7 +139,7 @@ It is natural at this point to want to transform the function into a method, as
139
139
}
140
140
~~~
141
141
142
-
However, this does not compile.
142
+
However, this does not compile.
143
143
Only monomorphic eta-expansion is applied, leading to the same issue as with our previous `Tuple.map` example.
@@ -251,7 +251,7 @@ For example, if the syntax of the language is changed, this section should list
251
251
252
252
Before we go on, it is important to clarify what we mean by "polymorphic method", we do not mean, as one would expect, "a method taking at least one type parameter clause", but rather "a (potentially partially applied) method whose next clause is a type clause", here is an example to illustrate:
253
253
254
-
~~~scala
254
+
~~~scala
255
255
extension (x: Int)
256
256
defpoly[T](x: T):T= x
257
257
// signature: (Int)[T](T): T
@@ -279,15 +279,15 @@ Note: Polymorphic functions always take term parameters (but `k` can equal zero
279
279
1. Copies of `T_i`s are created, and replaced in `U_i`s, `L_i`s, `A_i`s and `R`, noted respectively `T'_i`, `U'_i`, `L'_i`, `A'_i` and `R'`.
280
280
281
281
2. Is the expected type a polymorphic context function ?
282
-
*1. If yes then `m` is replaced by the following:
282
+
*1. If yes then `m` is replaced by the following:
283
283
~~~scala
284
-
[T'_1<:U'_1>:L'_1, ... , T'_n<:U'_n>:L'_n]
284
+
[T'_1<:U'_1>:L'_1, ... , T'_n<:U'_n>:L'_n]
285
285
=> (a_1: A'_1 ..., a_k: A'_k)
286
286
?=> m[T'_1, ..., T'_n]
287
287
~~~
288
-
*2. If no then `m` is replaced by the following:
288
+
*2. If no then `m` is replaced by the following:
289
289
~~~scala
290
-
[T'_1<:U'_1>:L'_1, ... , T'_n<:U'_n>:L'_n]
290
+
[T'_1<:U'_1>:L'_1, ... , T'_n<:U'_n>:L'_n]
291
291
=> (a_1: A'_1 ..., a_k: A'_k)
292
292
=> m[T'_1, ..., T'_n](a_1, ..., a_k)
293
293
~~~
@@ -321,7 +321,7 @@ extension [A](x: A)
321
321
deffoo[B](y: B) = (x, y)
322
322
323
323
valvoo: [T] =>T=> [U] =>U=> (T, U) = foo
324
-
// foo expands to:
324
+
// foo expands to:
325
325
// [T'] => (t: T') => ( foo[T'](t) with expected type [U] => U => (T', U) )
0 commit comments