Skip to content

Commit d665502

Browse files
authored
Update 2017-01-11-refer-other-arguments-in-args.md
1 parent e38ca6d commit d665502

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

sips/pending/_posts/2017-01-11-refer-other-arguments-in-args.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
---
22
layout: sip
33
disqus: true
4-
title: SIP-NN - SIP Title
4+
title: SIP-NN - Allow referring to other arguments in default parameters
55
---
66

77
**By: Pathikrit Bhowmick**
88

99
## History
1010

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 |
1415

1516
## Introduction
1617
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
2728

2829
However, the above workaround is not always suitable in certain situations.
2930

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+
3040
### Proposal
3141
Allow to refer to ***any*** parameters in the same (or left) curried parameter list:
3242
```scala
@@ -44,21 +54,21 @@ class Substring(s: String, start: Int = 0)(end: Int = s.length) // Legal
4454
class Substring(start: Int = 0, end: Int = s.length)(s: String) // Illegal
4555
```
4656

47-
We should also be able to refer to ***multiple*** paramaeters:
57+
We should also be able to refer to ***multiple*** parameters:
4858
```scala
4959
def binarySearch(start: Int, end: Int, middle: Int = (start + end)/2) // Legal
5060
```
5161

5262
## Interactions with other syntax
5363

5464
#### 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:
5666
```scala
5767
def substring(s: String, start: Int = 0, end: Int = s.length)
5868

5969
substring(_, start = 0, end = 5) // Legal
6070
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)
6272
```
6373

6474
#### Multiple Implicit Parameters
@@ -87,4 +97,4 @@ The following languages allow referring to previously declared arguments in the
8797
AFAIK, there are no major languages where referring to parameters declared to the ***right*** is allowed.
8898

8999
### 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

Comments
 (0)