From 5e5319f7fc1254903d06dc61986de9b771590c32 Mon Sep 17 00:00:00 2001 From: Seth Tisue Date: Tue, 31 Oct 2017 11:01:31 -0700 Subject: [PATCH] copyedit and fixes to "polymorphic methods" section of tour the most important change here is to omit the incorrect claim about Java at the end. but I also copyedited throughout. --- _tour/polymorphic-methods.md | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/_tour/polymorphic-methods.md b/_tour/polymorphic-methods.md index 6264399c1a..03a2e814b2 100644 --- a/_tour/polymorphic-methods.md +++ b/_tour/polymorphic-methods.md @@ -15,21 +15,22 @@ prerequisite-knowledge: unified-types redirect_from: "/tutorials/tour/polymorphic-methods.html" --- -Methods in Scala can be parameterized by type as well as value. The syntax is similar to that of generic classes. Type parameters are declared within a pair of brackets while value parameters are enclosed in a pair of parentheses. +Methods in Scala can be parameterized by type as well as value. The syntax is similar to that of generic classes. Type parameters are enclosed in square brackets, while value parameters are enclosed in parentheses. Here is an example: ```tut -def listOfDuplicates[A](x: A, length: Int): List[A] = { - if (length < 1) - Nil - else - x :: listOfDuplicates(x, length - 1) -} +def listOfDuplicates[A](x: A, length: Int): List[A] = + if (length < 1) + Nil + else + x :: listOfDuplicates(x, length - 1) println(listOfDuplicates[Int](3, 4)) // List(3, 3, 3, 3) println(listOfDuplicates("La", 8)) // List(La, La, La, La, La, La, La, La) ``` -The method `listOfDuplicates` takes a type parameter `A` and values parameters `x` and `length`. In this case, value `x` is of type `A`. If `length < 1` we return an empty list. Otherwise we prepend `x` to the the list of duplicates returned by the recursive call to `listOfDuplicates`. (note: `::` means prepend an element on the left to a sequence on the right). +The method `listOfDuplicates` takes a type parameter `A` and value parameters `x` and `length`. Value `x` is of type `A`. If `length < 1` we return an empty list. Otherwise we prepend `x` to the the list of duplicates returned by the recursive call. (Note that `::` means prepend an element on the left to a list on the right.) -When we call `listOfDuplicates` with `[Int]` as the type parameter, the first argument must be an int and the return type will be List[Int]. However, you don't always need to explicitly provide the the type parameter because the compiler can often figure it out based on the type of value argument (`"La"` is a String). In fact, if calling this method from Java it is impossible to provide the type parameter. +In first example call, we explicitly provide the type parameter by writing `[Int]`. Therefore the first argument must be an `Int` and the return type will be `List[Int]`. + +The second example call shows that you don't always need to explicitly provide the type parameter. The compiler can often infer it based on context or on the types of the value arguments. In this example, `"La"` is a `String` so the compiler knows `A` must be `String`.