1
1
---
2
2
layout : sip
3
3
disqus : true
4
- title : SIP-NN - SIP Title
4
+ title : SIP-NN - Allow referring to other arguments in default parameters
5
5
---
6
6
7
7
** By: Pathikrit Bhowmick**
8
8
9
9
## History
10
10
11
- | Date | Version |
12
- | ---------------| ---------------|
13
- | Jan 11th 2017 | Initial Draft |
11
+ | Date | Version |
12
+ | ---------------| ------------------|
13
+ | Jan 11th 2017 | Initial Draft |
14
+ | Jan 12th 2017 | Initial Feedback |
14
15
15
16
## Introduction
16
17
Currently there is no way to refer to other arguments in the default parameters list:
@@ -27,6 +28,15 @@ def substring(s: String, start: Int = 0)(end: Int = s.length): String
27
28
28
29
However, the above workaround is not always suitable in certain situations.
29
30
31
+ The other more verbose alternative is by overloading:
32
+
33
+ ``` scala
34
+ def substring (s : String , start : Int = 0 , end : Int = s.length): String
35
+ = substring(s, start = 0 , end = s.length)
36
+ def substring (s : String , start : Int , end : Int ): String
37
+ ```
38
+
39
+
30
40
### Proposal
31
41
Allow to refer to *** any*** parameters in the same (or left) curried parameter list:
32
42
``` scala
@@ -44,21 +54,21 @@ class Substring(s: String, start: Int = 0)(end: Int = s.length) // Legal
44
54
class Substring (start : Int = 0 , end : Int = s.length)(s : String ) // Illegal
45
55
```
46
56
47
- We should also be able to refer to *** multiple*** paramaeters :
57
+ We should also be able to refer to *** multiple*** parameters :
48
58
``` scala
49
59
def binarySearch (start : Int , end : Int , middle : Int = (start + end)/ 2 ) // Legal
50
60
```
51
61
52
62
## Interactions with other syntax
53
63
54
64
#### Partially Applied Functions:
55
- It must be required to specify the default arguments if the default argument's reference is unapplied :
65
+ Works as expected :
56
66
``` scala
57
67
def substring (s : String , start : Int = 0 , end : Int = s.length)
58
68
59
69
substring(_, start = 0 , end = 5 ) // Legal
60
70
substring(_, end = 5 ) // Legal (start = 0)
61
- substring(_, start = 0 ) // Illegal (need to declare end)
71
+ substring(_, start = 5 ) // Legal (same as s => substring(s, start = 5, end = s.length )
62
72
```
63
73
64
74
#### Multiple Implicit Parameters
@@ -87,4 +97,4 @@ The following languages allow referring to previously declared arguments in the
87
97
AFAIK, there are no major languages where referring to parameters declared to the *** right*** is allowed.
88
98
89
99
### Discussions
90
- [ Scala Lang Forum] ( https://contributors.scala-lang.org/t/refer-to-previous-argument-in-default-argument-list/215/6 )
100
+ [ Scala Lang Forum] ( https://contributors.scala-lang.org/t/refer-to-previous-argument-in-default-argument-list/215/6 )
0 commit comments