diff --git a/_ba/tour/packages-and-imports.md b/_ba/tour/packages-and-imports.md index e027c1bbc6..b466c5946a 100644 --- a/_ba/tour/packages-and-imports.md +++ b/_ba/tour/packages-and-imports.md @@ -9,6 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments +next-page: type-inference --- # Packages and Imports diff --git a/_ba/tour/type-inference.md b/_ba/tour/type-inference.md new file mode 100644 index 0000000000..73056dba5d --- /dev/null +++ b/_ba/tour/type-inference.md @@ -0,0 +1,15 @@ +--- +layout: tour +title: Type Inference +language: ba + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_es/tour/packages-and-imports.md b/_es/tour/packages-and-imports.md index 65e2a02816..6f1e51d951 100644 --- a/_es/tour/packages-and-imports.md +++ b/_es/tour/packages-and-imports.md @@ -9,6 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments +next-page: type-inference --- # Packages and Imports diff --git a/_es/tour/type-inference.md b/_es/tour/type-inference.md new file mode 100644 index 0000000000..65a2017fac --- /dev/null +++ b/_es/tour/type-inference.md @@ -0,0 +1,15 @@ +--- +layout: tour +title: Type Inference +language: es + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_ko/tour/packages-and-imports.md b/_ko/tour/packages-and-imports.md index 20123726cf..3a052dfd64 100644 --- a/_ko/tour/packages-and-imports.md +++ b/_ko/tour/packages-and-imports.md @@ -9,6 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments +next-page: type-inference --- # Packages and Imports diff --git a/_ko/tour/type-inference.md b/_ko/tour/type-inference.md new file mode 100644 index 0000000000..bc56d9b6d0 --- /dev/null +++ b/_ko/tour/type-inference.md @@ -0,0 +1,15 @@ +--- +layout: tour +title: Type Inference +language: ko + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_pl/tour/packages-and-imports.md b/_pl/tour/packages-and-imports.md index e1ae1b2626..724704030b 100644 --- a/_pl/tour/packages-and-imports.md +++ b/_pl/tour/packages-and-imports.md @@ -9,6 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments +next-page: type-inference --- # Packages and Imports diff --git a/_pl/tour/type-inference.md b/_pl/tour/type-inference.md new file mode 100644 index 0000000000..0a8ecdeba1 --- /dev/null +++ b/_pl/tour/type-inference.md @@ -0,0 +1,15 @@ +--- +layout: tour +title: Type Inference +language: pl + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_pt-br/tour/packages-and-imports.md b/_pt-br/tour/packages-and-imports.md index 7c95767e39..abc74a9f2a 100644 --- a/_pt-br/tour/packages-and-imports.md +++ b/_pt-br/tour/packages-and-imports.md @@ -9,6 +9,7 @@ partof: scala-tour num: 35 previous-page: named-arguments +next-page: type-inference --- # Packages and Imports diff --git a/_pt-br/tour/type-inference.md b/_pt-br/tour/type-inference.md new file mode 100644 index 0000000000..2426517038 --- /dev/null +++ b/_pt-br/tour/type-inference.md @@ -0,0 +1,15 @@ +--- +layout: tour +title: Type Inference +language: pt-br + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +(this section of the tour has not been translated yet. pull request +with translation welcome!) diff --git a/_tour/packages-and-imports.md b/_tour/packages-and-imports.md index 79e4c2218c..124b7c9d55 100644 --- a/_tour/packages-and-imports.md +++ b/_tour/packages-and-imports.md @@ -8,6 +8,7 @@ partof: scala-tour num: 35 previous-page: named-arguments +next-page: type-inference --- # Packages and Imports diff --git a/_tour/type-inference.md b/_tour/type-inference.md new file mode 100644 index 0000000000..782dfd62c7 --- /dev/null +++ b/_tour/type-inference.md @@ -0,0 +1,73 @@ +--- +layout: tour +title: Type Inference + +discourse: true + +partof: scala-tour + +num: 36 +previous-page: packages-and-imports +--- + +The Scala compiler can often infer the type of an expression so you don't have to declare it explicitly. + +## Omitting the type + +```tut +val businessName = "Montreux Jazz Café" +``` +The compiler can detect that `businessName` is a String. It works similarly with methods: + +```tut +def squareOf(x: Int) = x * x +``` +The compiler can infer that the return type is an `Int`, so no explicit return type is required. + +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: + +```tut:fail +def fac(n: Int) = if (n == 0) 1 else n * fac(n - 1) +``` + +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. + +Here are two examples: + +```tut +case class MyPair[A, B](x: A, y: B); +val p = MyPair(1, "scala") // type: MyPair[Int, String] + +def id[T](x: T) = x +val q = id(1) // type: Int +``` + +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`. + +## Parameters + +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. + +```tut +Seq(1, 3, 4).map(x => x * 2) // List(2, 6, 8) +``` + +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`. + +## When _not_ to rely on type inference + +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. + +Also, type inference can sometimes infer a too-specific type. Suppose we write: + +```tut +var obj = null +``` + +Then we can't then go on and make this reassignment: + +```tut:fail +obj = new AnyRef +``` + +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.