Skip to content

Commit c81471b

Browse files
committed
Add amendments for quoted patterns.
1 parent bd103cf commit c81471b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

content/alternative-bind-variables.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ We can, of course, work around it by hoisting the logic to a helper function to
102102
~~~ scala
103103
def loop(cmd: Cmd): Unit =
104104
def pickUp(item: String): Unit = // Code for picking up item
105-
cmd match
106-
case Command(Pick :: Up :: Item(name)) => pickUp(name)
107-
case Command(Get :: Item(name)) => pickUp(name)
105+
cmd match
106+
case Command(Pick :: Up :: Item(name)) => pickUp(name)
107+
case Command(Get :: Item(name)) => pickUp(name)
108108
~~~
109109

110110
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
240240

241241
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`.
242242

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+
243247
### Alternatives
244248

245249
#### Enforcing a single type for a bound variable
@@ -287,7 +291,7 @@ We propose that the following clauses be added to the specification:
287291

288292
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.
289293

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`$.
291295

292296
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.
293297

@@ -315,6 +319,12 @@ The associated [thread](https://contributors.scala-lang.org/t/pre-sip-bind-varia
315319
The author has a current in-progress implementation focused on the typer which compiles the examples with the expected types. Interested
316320
parties are welcome to see the WIP [here](https://github.com/lampepfl/dotty/compare/main...yilinwei:dotty:main).
317321

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+
318328
## Acknowledgements
319329

320330
Many thanks to **Zainab Ali** for proof-reading the draft, **Nicolas Stucki** and **Guillaume Martres** for their pointers on the dotty

0 commit comments

Comments
 (0)