Skip to content

Commit 76fdb81

Browse files
authored
Merge pull request #985 from SethTisue/type-inference
add new language tour section on type inference
2 parents 0acd587 + 73d7763 commit 76fdb81

12 files changed

+154
-0
lines changed

_ba/tour/packages-and-imports.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ partof: scala-tour
99

1010
num: 35
1111
previous-page: named-arguments
12+
next-page: type-inference
1213
---
1314

1415
# Packages and Imports

_ba/tour/type-inference.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
layout: tour
3+
title: Type Inference
4+
language: ba
5+
6+
discourse: true
7+
8+
partof: scala-tour
9+
10+
num: 36
11+
previous-page: packages-and-imports
12+
---
13+
14+
(this section of the tour has not been translated yet. pull request
15+
with translation welcome!)

_es/tour/packages-and-imports.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ partof: scala-tour
99

1010
num: 35
1111
previous-page: named-arguments
12+
next-page: type-inference
1213
---
1314

1415
# Packages and Imports

_es/tour/type-inference.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
layout: tour
3+
title: Type Inference
4+
language: es
5+
6+
discourse: true
7+
8+
partof: scala-tour
9+
10+
num: 36
11+
previous-page: packages-and-imports
12+
---
13+
14+
(this section of the tour has not been translated yet. pull request
15+
with translation welcome!)

_ko/tour/packages-and-imports.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ partof: scala-tour
99

1010
num: 35
1111
previous-page: named-arguments
12+
next-page: type-inference
1213
---
1314

1415
# Packages and Imports

_ko/tour/type-inference.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
layout: tour
3+
title: Type Inference
4+
language: ko
5+
6+
discourse: true
7+
8+
partof: scala-tour
9+
10+
num: 36
11+
previous-page: packages-and-imports
12+
---
13+
14+
(this section of the tour has not been translated yet. pull request
15+
with translation welcome!)

_pl/tour/packages-and-imports.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ partof: scala-tour
99

1010
num: 35
1111
previous-page: named-arguments
12+
next-page: type-inference
1213
---
1314

1415
# Packages and Imports

_pl/tour/type-inference.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
layout: tour
3+
title: Type Inference
4+
language: pl
5+
6+
discourse: true
7+
8+
partof: scala-tour
9+
10+
num: 36
11+
previous-page: packages-and-imports
12+
---
13+
14+
(this section of the tour has not been translated yet. pull request
15+
with translation welcome!)

_pt-br/tour/packages-and-imports.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ partof: scala-tour
99

1010
num: 35
1111
previous-page: named-arguments
12+
next-page: type-inference
1213
---
1314

1415
# Packages and Imports

_pt-br/tour/type-inference.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
layout: tour
3+
title: Type Inference
4+
language: pt-br
5+
6+
discourse: true
7+
8+
partof: scala-tour
9+
10+
num: 36
11+
previous-page: packages-and-imports
12+
---
13+
14+
(this section of the tour has not been translated yet. pull request
15+
with translation welcome!)

_tour/packages-and-imports.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ partof: scala-tour
88

99
num: 35
1010
previous-page: named-arguments
11+
next-page: type-inference
1112
---
1213

1314
# Packages and Imports

_tour/type-inference.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
layout: tour
3+
title: Type Inference
4+
5+
discourse: true
6+
7+
partof: scala-tour
8+
9+
num: 36
10+
previous-page: packages-and-imports
11+
---
12+
13+
The Scala compiler can often infer the type of an expression so you don't have to declare it explicitly.
14+
15+
## Omitting the type
16+
17+
```tut
18+
val businessName = "Montreux Jazz Café"
19+
```
20+
The compiler can detect that `businessName` is a String. It works similarly with methods:
21+
22+
```tut
23+
def squareOf(x: Int) = x * x
24+
```
25+
The compiler can infer that the return type is an `Int`, so no explicit return type is required.
26+
27+
For recursive methods, the compiler is not able to infer a result type. Here is a program which will fail the compiler for this reason:
28+
29+
```tut:fail
30+
def fac(n: Int) = if (n == 0) 1 else n * fac(n - 1)
31+
```
32+
33+
It is also not compulsory to specify type parameters when [polymorphic methods](polymorphic-methods.html) are called or [generic classes](generic-classes.html) are instantiated. The Scala compiler will infer such missing type parameters from the context and from the types of the actual method/constructor parameters.
34+
35+
Here are two examples:
36+
37+
```tut
38+
case class MyPair[A, B](x: A, y: B);
39+
val p = MyPair(1, "scala") // type: MyPair[Int, String]
40+
41+
def id[T](x: T) = x
42+
val q = id(1) // type: Int
43+
```
44+
45+
The compiler uses the types of the arguments of `MyPair` to figure out what type `A` and `B` are. Likewise for the type of `x`.
46+
47+
## Parameters
48+
49+
The compiler never infers method parameter types. However, in certain cases, it can infer anonymous function parameter types when the function is passed as argument.
50+
51+
```tut
52+
Seq(1, 3, 4).map(x => x * 2) // List(2, 6, 8)
53+
```
54+
55+
The parameter for map is `f: A => B`. Because we put integers in the `Seq`, the compiler knows that `A` is `Int` (i.e. that `x` is an integer). Therefore, the compiler can infer from `x * 2` that `B` is type `Int`.
56+
57+
## When _not_ to rely on type inference
58+
59+
It is generally considered more readable to declare the type of members exposed in a public API. Therefore, we recommended that you make the type explicit for any APIs that will be exposed to users of your code.
60+
61+
Also, type inference can sometimes infer a too-specific type. Suppose we write:
62+
63+
```tut
64+
var obj = null
65+
```
66+
67+
Then we can't then go on and make this reassignment:
68+
69+
```tut:fail
70+
obj = new AnyRef
71+
```
72+
73+
It won't compile, because the type inferred for `obj` was `Null`. Since the only value of that type is `null`, it is impossible to assign a different value.

0 commit comments

Comments
 (0)