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
+14-4Lines changed: 14 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -102,9 +102,9 @@ We can, of course, work around it by hoisting the logic to a helper function to
102
102
~~~scala
103
103
defloop(cmd: Cmd):Unit=
104
104
defpickUp(item: String):Unit=// Code for picking up item
105
-
cmd match
106
-
caseCommand(Pick::Up::Item(name)) => pickUp(name)
107
-
caseCommand(Get::Item(name)) => pickUp(name)
105
+
cmd match
106
+
caseCommand(Pick::Up::Item(name)) => pickUp(name)
107
+
caseCommand(Get::Item(name)) => pickUp(name)
108
108
~~~
109
109
110
110
Or any number of different encodings. However, all of them are less intuitive and less obvious than the code we tried to write.
@@ -240,6 +240,10 @@ This begs the question of what to do in the case of an explicit `@` binding wher
240
240
241
241
To be consistent with the named bindings, we argue that the code should compile and a contextual variable added to the scope with the type of `String | Int`.
242
242
243
+
### Quoted patterns
244
+
245
+
[Quoted patterns](https://docs.scala-lang.org/scala3/guides/macros/quotes.html#quoted-patterns) will not be supported in this SIP and the behaviour of quoted patterns will remain the same as currently i.e. any quoted pattern appearing in an alternative pattern binding a variable or type variable will be rejected as illegal.
246
+
243
247
### Alternatives
244
248
245
249
#### Enforcing a single type for a bound variable
@@ -287,7 +291,7 @@ We propose that the following clauses be added to the specification:
287
291
288
292
Let $`p_1 | \ldots | p_n`$ be an alternative pattern at an arbitrary depth within a case pattern and $`\Gamma_n`$ is the named scope associated with each alternative.
289
293
290
-
Let the named variables introduced within each alternative $`p_n`$, be $`x_i \in \Gamma_n`$ and the unnamed contextual variables within each alternative have the type $`T_i \in \Gamma_n`$.
294
+
If `p_i` is a quoted pattern binding a variable or type variable, the alternative pattern is considered invalid. Otherwise, let the named variables introduced within each alternative $`p_n`$, be $`x_i \in \Gamma_n`$ and the unnamed contextual variables within each alternative have the type $`T_i \in \Gamma_n`$.
291
295
292
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.
293
297
@@ -315,6 +319,12 @@ The associated [thread](https://contributors.scala-lang.org/t/pre-sip-bind-varia
315
319
The author has a current in-progress implementation focused on the typer which compiles the examples with the expected types. Interested
316
320
parties are welcome to see the WIP [here](https://github.com/lampepfl/dotty/compare/main...yilinwei:dotty:main).
317
321
322
+
### Further work
323
+
324
+
#### Quoted patterns
325
+
326
+
More investigation is needed to see how quoted patterns with bind variables in alternative patterns.
327
+
318
328
## Acknowledgements
319
329
320
330
Many thanks to **Zainab Ali** for proof-reading the draft, **Nicolas Stucki** and **Guillaume Martres** for their pointers on the dotty
0 commit comments