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/2022-02-25-pattern-matching-with-named-fields.md
+31-14Lines changed: 31 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -114,7 +114,7 @@ For incomplete list of alternatives see [alternative desugaring](#alternative-de
114
114
Pro:
115
115
116
116
* 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
118
118
* reuse possible
119
119
120
120
Con:
@@ -168,6 +168,26 @@ But this leads to (arguably small) inconsistency, as pointed out by Lionel Parre
168
168
caseUser(age = _) =>"Just wanted to use the extractor, lol!"
169
169
```
170
170
171
+
### Reordering of user code
172
+
173
+
174
+
```scala
175
+
//TODO: Test this
176
+
objectUser:
177
+
classUserExtractor(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
+
typeNames= ("name", "age", "city")
183
+
// Ask for city, then for name
184
+
valUser(city = _, name = _) = user
185
+
// But the execution shows:
186
+
// Got name
187
+
// Got age
188
+
// Got city
189
+
```
190
+
171
191
## Alternatives
172
192
173
193
### Without any changes to the language
@@ -199,23 +219,25 @@ In the sprit of [name based pattern matching](https://dotty.epfl.ch/docs/referen
199
219
objectUser:
200
220
classUserMatching(user: User):
201
221
def_1= user.name
202
-
...
203
-
204
222
def_name= user.name
205
-
inlinedef_age= _2
223
+
224
+
@deprecatedName
225
+
def_oldName= user.name.toUpperCase
226
+
...
227
+
228
+
defunapply(user: User) =UserMatching(user)
206
229
```
207
230
208
231
Pro:
209
232
210
-
* allows to add more fields
233
+
* allows to have more named fields than positional fields
211
234
* allows `@deprecatedName`
212
-
* is the only desugaring, that doesn't use string literals
213
235
214
236
Con:
215
237
216
238
* 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.
219
241
220
242
#### Annotated `unapply` method
221
243
@@ -253,12 +275,7 @@ Con:
253
275
* 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.
254
276
* no clear way of encoding deprecated name
255
277
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
262
279
263
280
Lionel Parreaux proposed a more powerful mechanism, where if guards of cases could them self contains destructuring patterns.
0 commit comments