Skip to content

Commit e9d87e0

Browse files
committed
Add section on named type arguments
1 parent 1d4c392 commit e9d87e0

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

docs/docs/reference/named-typeargs.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
layout: doc-page
3+
title: "Named Type Arguments"
4+
---
5+
6+
Type arguments of methods can now be named as well as by position. Example:
7+
8+
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)
13+
14+
Similar to a named value argument `(x = e)`, a named type argument
15+
`[X = T]` instantiates the type parameter `X` to the type `T`. Type
16+
arguments must be all named or un-named, mixtures of named and
17+
positional type arguments are not supported.
18+
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 have
24+
to be given always for all type parameters.
25+

docs/sidebar.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ sidebar:
2323
url: docs/reference/implicit-by-name-parameters.htmldocs/reference/implicit-by-name-parameters.html
2424
- title: Auto Parameter Tupling
2525
url: docs/reference/auto-parameter-tupling.html
26+
- title: Named Type Arguments
27+
url: docs/reference/named-typeargs.html
2628
- title: Usage
2729
subsection:
2830
- title: sbt-projects
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package namedTypeArgsR {
2+
3+
object t1 {
4+
5+
def construct[Elem, Coll[_]](xs: Elem*): Coll[Elem] = ???
6+
7+
val xs3 = construct[Coll = List](1, 2, 3)
8+
9+
val xs2 = construct[Coll = List, Elem = Int](1, 2, 3)
10+
11+
}
12+
13+
}

0 commit comments

Comments
 (0)