@@ -185,7 +185,7 @@ can be applied to methods `def`s, `class` constructors, or `case class`es to gen
185
185
customName : String = null ,
186
186
customDoc : String = null ,
187
187
@ unroll sorted : Boolean = true ,
188
- nameMapper : String => Option [String ] = Util .kebabCaseNameMapper
188
+ @ unroll nameMapper : String => Option [String ] = Util .kebabCaseNameMapper
189
189
): Either [String , T ] = ???
190
190
```
191
191
@@ -238,7 +238,7 @@ parameter that was added (in this case, `b: Boolean = true`)
238
238
import scala .annotation .unroll
239
239
240
240
object Unrolled {
241
- def foo (s : String , n : Int = 1 , @ unroll b : Boolean = true , l : Long = 0 ) = s + n + b + l
241
+ def foo (s : String , n : Int = 1 , @ unroll b : Boolean = true , @ unroll l : Long = 0 ) = s + n + b + l
242
242
}
243
243
```
244
244
@@ -248,10 +248,8 @@ These forwarders do nothing but forward the call to the current implementation,
248
248
given default parameter values:
249
249
250
250
``` scala
251
- import scala .annotation .unroll
252
-
253
251
object Unrolled {
254
- def foo (s : String , n : Int = 1 , @ unroll b : Boolean = true , l : Long = 0 ) = s + n + b + l
252
+ def foo (s : String , n : Int = 1 , @ unroll b : Boolean = true , @ unroll l : Long = 0 ) = s + n + b + l
255
253
256
254
def foo (s : String , n : Int , b : Boolean ) = foo(s, n, b, 0 )
257
255
def foo (s : String , n : Int ) = foo(s, n, true , 0 )
@@ -262,15 +260,27 @@ As a result, old callers who expect `def foo(String, Int, Boolean)` or `def foo(
262
260
can continue to work, even as new parameters are added to ` def foo ` . The only restriction is that
263
261
new parameters can only be added on the right, and they must be provided with a default value.
264
262
263
+ If multiple default parameters are added at once (e.g. ` b ` and ` l ` below) you can also
264
+ choose to only ` @unroll ` the first default parameter of each batch, to avoid generating
265
+ unnecessary forwarders:
266
+
267
+ ``` scala
268
+ object Unrolled {
269
+ def foo (s : String , n : Int = 1 , @ unroll b : Boolean = true , l : Long = 0 ) = s + n + b + l
270
+
271
+ def foo (s : String , n : Int ) = foo(s, n, true , 0 )
272
+ }
273
+ ```
274
+
265
275
If there are multiple parameter lists (e.g. for curried methods or methods taking implicits) only one
266
276
parameter list can be unrolled (though it does not need to be the first one). e.g. this works:
267
277
268
278
``` scala
269
279
object Unrolled {
270
280
def foo (s : String ,
271
281
n : Int = 1 ,
272
- @ unroll b : Boolean = true ,
273
- l : Long = 0 )
282
+ @ unroll b : Boolean = true ,
283
+ @ unroll l : Long = 0 )
274
284
(implicit blah : Blah ) = s + n + b + l
275
285
}
276
286
```
@@ -282,8 +292,8 @@ object Unrolled{
282
292
def foo (blah : Blah )
283
293
(s : String ,
284
294
n : Int = 1 ,
285
- @ unroll b : Boolean = true ,
286
- l : Long = 0 ) = s + n + b + l
295
+ @ unroll b : Boolean = true ,
296
+ @ unroll l : Long = 0 ) = s + n + b + l
287
297
}
288
298
```
289
299
@@ -295,19 +305,15 @@ Class constructors and secondary constructors are treated by `@unroll` just like
295
305
other method:
296
306
297
307
``` scala
298
- import scala .annotation .unroll
299
-
300
- class Unrolled (s : String , n : Int = 1 , @ unroll b : Boolean = true , l : Long = 0 ){
308
+ class Unrolled (s : String , n : Int = 1 , @ unroll b : Boolean = true , @ unroll l : Long = 0 ){
301
309
def foo = s + n + b + l
302
310
}
303
311
```
304
312
305
313
Unrolls to:
306
314
307
315
``` scala
308
- import scala .annotation .unroll
309
-
310
- class Unrolled (s : String , n : Int = 1 , @ unroll b : Boolean = true , l : Long = 0 ){
316
+ class Unrolled (s : String , n : Int = 1 , @ unroll b : Boolean = true , @ unroll l : Long = 0 ){
311
317
def foo = s + n + b + l
312
318
313
319
def this (s : String , n : Int , b : Boolean ) = this (s, n, b, 0 )
@@ -318,12 +324,10 @@ class Unrolled(s: String, n: Int = 1, @unroll b: Boolean = true, l: Long = 0){
318
324
### Unrolling ` class ` secondary constructors
319
325
320
326
``` scala
321
- import scala .annotation .unroll
322
-
323
327
class Unrolled () {
324
328
var foo = " "
325
329
326
- def this (s : String , n : Int = 1 , @ unroll b : Boolean = true , l : Long = 0 ) = {
330
+ def this (s : String , n : Int = 1 , @ unroll b : Boolean = true , @ unroll l : Long = 0 ) = {
327
331
this ()
328
332
foo = s + n + b + l
329
333
}
@@ -333,12 +337,10 @@ class Unrolled() {
333
337
Unrolls to:
334
338
335
339
``` scala
336
- import scala .annotation .unroll
337
-
338
340
class Unrolled () {
339
341
var foo = " "
340
342
341
- def this (s : String , n : Int = 1 , @ unroll b : Boolean = true , l : Long = 0 ) = {
343
+ def this (s : String , n : Int = 1 , @ unroll b : Boolean = true , @ unroll l : Long = 0 ) = {
342
344
this ()
343
345
foo = s + n + b + l
344
346
}
@@ -357,8 +359,6 @@ generates forwarders for those methods as well, based on the presence of the
357
359
` @unroll ` annotation in the primary constructor:
358
360
359
361
``` scala
360
- import scala .annotation .unroll
361
-
362
362
case class Unrolled (s : String , n : Int = 1 , @ unroll b : Boolean = true ){
363
363
def foo = s + n + b
364
364
}
@@ -367,9 +367,7 @@ case class Unrolled(s: String, n: Int = 1, @unroll b: Boolean = true){
367
367
Unrolls to:
368
368
369
369
``` scala
370
- import scala .annotation .unroll
371
-
372
- case class Unrolled (s : String , n : Int = 1 , @ unroll b : Boolean = true , l : Long = 0L ){
370
+ case class Unrolled (s : String , n : Int = 1 , @ unroll b : Boolean = true , @ unroll l : Long = 0L ){
373
371
def this (s : String , n : Int ) = this (s, n, true , 0L )
374
372
def this (s : String , n : Int , b : Boolean ) = this (s, n, b, 0L )
375
373
@@ -490,7 +488,7 @@ against different versions of each other (hence the varying number of parameters
490
488
491
489
``` scala
492
490
class Upstream { // V2
493
- def foo (s : String , n : Int = 1 , @ unroll b : Boolean = true , l : Long = 0 ) = s + n + b + l
491
+ def foo (s : String , n : Int = 1 , @ unroll b : Boolean = true , @ unroll l : Long = 0 ) = s + n + b + l
494
492
}
495
493
```
496
494
@@ -624,48 +622,29 @@ using the same implementation and same user-facing semantics.
624
622
## Minor Alternatives:
625
623
626
624
627
- ### ` @unrollOnly `
625
+ ### ` @unrollAll `
628
626
629
- Currently, ` @unroll ` generates forwarders for every default parameter to the right of the one
630
- annotated. This is not always necessary, e.g. if multiple parameters are added at once, we
631
- should only need a single forwarder for the entire set. This can be supported by requiring
632
- supporting an ` @unrollOnly ` is provided for every default parameter that needs a forwarder generated. That
633
- would mean generating two forwarders would look like this:
627
+ Currently, ` @unroll ` generates a forwarder only for the annotated default parameter;
628
+ if you want to generate multiple forwarders, you need to ` @unroll ` each one. In the
629
+ vast majority of scenarios, we want to unroll every default parameters we add, and in
630
+ many cases default parameters are added one at a time. In this case, an ` @unrollAll `
631
+ annotation may be useful, a shorthand for applying ` @unroll ` to the annotated default
632
+ parameter and every parameter to the right of it:
634
633
635
634
``` scala
636
635
object Unrolled {
637
- def foo (s : String , n : Int = 1 , @ unrollOnly b : Boolean = true , @ unrollOnly l : Long = 0 ) = s + n + b + l
636
+ def foo (s : String , n : Int = 1 , @ unrollAll b : Boolean = true , l : Long = 0 ) = s + n + b + l
638
637
}
639
638
```
640
639
``` scala
641
640
object Unrolled {
642
- def foo (s : String , n : Int = 1 , @ unrollOnly b : Boolean = true , @ unrollOnly l : Long = 0 ) = s + n + b + l
641
+ def foo (s : String , n : Int = 1 , b : Boolean = true , l : Long = 0 ) = s + n + b + l
643
642
644
643
def foo (s : String , n : Int , b : Boolean ) = foo(s, n, b, 0 )
645
644
def foo (s : String , n : Int ) = foo(s, n, true , 0 )
646
645
}
647
646
```
648
647
649
- And generating one forwarder would look like this:
650
-
651
- ``` scala
652
- object Unrolled {
653
- def foo (s : String , n : Int = 1 , @ unrollOnly b : Boolean = true , l : Long = 0 ) = s + n + b + l
654
- }
655
- ```
656
- ``` scala
657
- object Unrolled {
658
- def foo (s : String , n : Int = 1 , @ unrollOnly b : Boolean = true , l : Long = 0 ) = s + n + b + l
659
-
660
- def foo (s : String , n : Int ) = foo(s, n, true , 0 )
661
- }
662
- ```
663
-
664
- ` @unrollOnly ` would provide a more granular replacement for ` @unroll ` . This probably does not
665
- make a huge difference in most cases, but it could be useful in scenarios where there
666
- are a large number of default parameters being added every version, as it would minimize the
667
- amount of generated code.
668
-
669
648
670
649
### Abstract Methods
671
650
0 commit comments