Skip to content

Commit 226573e

Browse files
committed
Initial example about execution order
1 parent 8bb0f10 commit 226573e

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

_sips/sips/2022-02-25-pattern-matching-with-named-fields.md

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ For incomplete list of alternatives see [alternative desugaring](#alternative-de
114114
Pro:
115115

116116
* with shapeless records we have a good understanding what we are doing, however we need to tweak the encoding a bit
117-
* with some helper definitions the usage would be quite short
117+
* with some helper definitions the usage could be quite short
118118
* reuse possible
119119

120120
Con:
@@ -168,6 +168,26 @@ But this leads to (arguably small) inconsistency, as pointed out by Lionel Parre
168168
case User(age = _) => "Just wanted to use the extractor, lol!"
169169
```
170170

171+
### Reordering of user code
172+
173+
174+
```scala
175+
//TODO: Test this
176+
object User:
177+
class UserExtractor(user: User):
178+
def _1 = println("Got name"); user.name
179+
def _2 = println("Got age"); user.age
180+
def _3 = println("Got city"); user.city
181+
182+
type Names = ("name", "age", "city")
183+
// Ask for city, then for name
184+
val User(city = _, name = _) = user
185+
// But the execution shows:
186+
// Got name
187+
// Got age
188+
// Got city
189+
```
190+
171191
## Alternatives
172192

173193
### Without any changes to the language
@@ -199,23 +219,25 @@ In the sprit of [name based pattern matching](https://dotty.epfl.ch/docs/referen
199219
object User:
200220
class UserMatching(user: User):
201221
def _1 = user.name
202-
...
203-
204222
def _name = user.name
205-
inline def _age = _2
223+
224+
@deprecatedName
225+
def _oldName = user.name.toUpperCase
226+
...
227+
228+
def unapply(user: User) = UserMatching(user)
206229
```
207230

208231
Pro:
209232

210-
* allows to add more fields
233+
* allows to have more named fields than positional fields
211234
* allows `@deprecatedName`
212-
* is the only desugaring, that doesn't use string literals
213235

214236
Con:
215237

216238
* How to detect that a name means the same as a position? Maybe detect simple patterns like the last line in the example?
217-
* long and verbose, without any shortcuts in sight
218-
* An underscore at the beginning of a name is an unheard of pattern, even in Scala. This could accidentally expose fields, which weren't suppose to become fields.
239+
* It's long and verbose, without any shortcuts in sight.
240+
* An underscore at the beginning of a name isn't an unheard of pattern, even in Scala. This could accidentally expose fields, which weren't suppose to become fields.
219241

220242
#### Annotated `unapply` method
221243

@@ -253,12 +275,7 @@ Con:
253275
* the type and the method can be defined in unrelated traits. Only at the use site of the can be checked if the type and the method agree on the arity of the pattern.
254276
* no clear way of encoding deprecated name
255277

256-
### Named Tuple Arguments / Anonymous Case Classes
257-
258-
This was mentioned in the discussion about [Named Tuple Arguments / Anonymous Case Classes][named-tuple] as bonus, that named tuples could transport the names from unapply to the pattern.
259-
This would be more generic and could handle user defined extractors.
260-
261-
### Partial destructuring in guards
278+
#### Partial destructuring in guards
262279

263280
Lionel Parreaux proposed a more powerful mechanism, where if guards of cases could them self contains destructuring patterns.
264281

0 commit comments

Comments
 (0)