Skip to content

Commit 1fe58b2

Browse files
committed
More details on named type arguments
1 parent 05ddfda commit 1fe58b2

File tree

4 files changed

+79
-10
lines changed

4 files changed

+79
-10
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
layout: doc-page
3+
title: "Named Type Arguments - More Details"
4+
---
5+
6+
## Syntax
7+
8+
The addition to the grammar is:
9+
10+
```
11+
SimpleExpr1 ::= ...
12+
| SimpleExpr (TypeArgs | NamedTypeArgs)
13+
NamedTypeArgs ::= ‘[’ NamedTypeArg {‘,’ NamedTypeArg} ‘]’
14+
NamedTypeArg ::= id ‘=’ Type
15+
```
16+
17+
Note in particular that named arguments cannot be passed to type constructors:
18+
19+
``` scala
20+
class C[T]
21+
22+
val x: C[T = Int] = // error
23+
new C[T = Int] // error
24+
25+
class E extends C[T = Int] // error
26+
```
27+
28+
## Multiple argument lists
29+
30+
When arguments for some parameters are omitted, it's possible to specify the
31+
remaining arguments using another argument list:
32+
33+
```scala
34+
def construct[Elem, Coll[_]](xs: Elem*): Coll[Elem] = ???
35+
36+
construct[Coll = List][Elem = Int](1, 2, 3)
37+
construct[Coll = List][Int](1, 2, 3)
38+
```
39+
40+
It's not possible to specify the same argument multiple times this way:
41+
42+
```scala
43+
construct[Coll = List][Coll = List](1, 2, 3) // error
44+
```
45+
46+
## Compatibility considerations
47+
48+
Named type arguments do not have an impact on binary compatibility, but they
49+
have an impact on source compatibility: if the name of a method type parameter
50+
is changed, any existing named reference to this parameter will break. This
51+
means that the names of method type parameters are now part of the public API
52+
of a library.
53+
54+
(Unimplemented proposal: to mitigate this,
55+
[`scala.deprecatedName`](https://www.scala-lang.org/api/current/scala/deprecatedName.html)
56+
could be extended to also be applicable on method type parameters.)

docs/docs/reference/named-typeargs.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ title: "Named Type Arguments"
55

66
Type arguments of methods can now be named, as well as by position. Example:
77

8+
``` scala
9+
def construct[Elem, Coll[_]](xs: Elem*): Coll[Elem] = ???
810

9-
def construct[Elem, Coll[_]](xs: Elem*): Coll[Elem] = ???
10-
11-
val xs2 = construct[Coll = List, Elem = Int](1, 2, 3)
12-
val xs3 = construct[Coll = List](1, 2, 3)
11+
val xs1 = construct[Coll = List, Elem = Int](1, 2, 3)
12+
val xs2 = construct[Coll = List](1, 2, 3)
13+
```
1314

1415
Similar to a named value argument `(x = e)`, a named type argument
1516
`[X = T]` instantiates the type parameter `X` to the type `T`. Type
1617
arguments must be all named or un-named, mixtures of named and
1718
positional type arguments are not supported.
1819

19-
The main benefit of named type arguments is that they allow some
20-
arguments to be omitted. Indeed, if type arguments are named, some
21-
arguments may be left out. An example is the definition of `xs3`
22-
above. A missing type argument is inferred as usual by local type
23-
inference. The same is not true for positional arguments, which must always
24-
be provided for all type parameters.
20+
## Motivation
21+
22+
The main benefit of named type arguments is that unlike positional arguments,
23+
you are allowed to omit passing arguments for some parameters, like in the
24+
definition of `xs2` above. A missing type argument is inferred as usual by
25+
local type inference. This is particularly useful in situations where some type
26+
arguments can be easily inferred from others.
2527

28+
[More details](./named-typeargs-spec.html)

tests/neg/namedTypeParams.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ object Test {
99
class E extends C[T = Int] // error: ']' expected, but `=` found // error
1010
class F extends C[T = Int]() // error: ']' expected, but `=` found // error
1111

12+
def f[X, Y](x: X, y: Y): Int = ???
13+
14+
f[X = Int, String](1, "") // error
15+
f[X = Int][X = Int][Y = String](1, "") // error
1216
}

tests/pos/namedTypeParams.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ object Test {
66
f[X = Int, Y = String](1, "")
77
f[X = Int](1, "")
88
f[Y = String](1, "")
9+
10+
f[X = Int][Y = String](1, "")
11+
f[X = Int][String](1, "")
12+
13+
f[Y = String][X = Int](1, "")
14+
f[Y = String][Int](1, "")
915
}

0 commit comments

Comments
 (0)