You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: tutorials/tour/implicit-parameters.md
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ The actual arguments that are eligible to be passed to an implicit parameter fal
19
19
20
20
In the following example we define a method `sum` which computes the sum of a list of elements using the monoid's `add` and `unit` operations. Please note that implicit values can not be top-level, they have to be members of a template.
21
21
22
-
/** This example uses a structure from abstract algebra to show how implicit parameters work. A semigroup is an algebraic structure on a set A with an (associative) operation, suggestively called add here, that combines an A pair and returns another A. */
22
+
/** This example uses a structure from abstract algebra to show how implicit parameters work. A semigroup is an algebraic structure on a set A with an (associative) operation, called add here, that combines a pair of A's and returns another A. */
23
23
abstract class SemiGroup[A] {
24
24
def add(x: A, y: A): A
25
25
}
@@ -28,7 +28,7 @@ In the following example we define a method `sum` which computes the sum of a li
28
28
def unit: A
29
29
}
30
30
object ImplicitTest extends App {
31
-
/** To show how implicit parameters work we first give monoids that are specialized to strings and integers. The keyword implicit here is used to indicate that the corresponding object can be used implicitly, within the same scope, as a parameter of a function marked implicit. */
31
+
/** To show how implicit parameters work, we first define monoids for strings and integers. The implicit keyword indicates that the corresponding object can be used implicitly, within this scope, as a parameter of a function marked implicit. */
def add(x: String, y: String): String = x concat y
34
34
def unit: String = ""
@@ -37,12 +37,12 @@ In the following example we define a method `sum` which computes the sum of a li
37
37
def add(x: Int, y: Int): Int = x + y
38
38
def unit: Int = 0
39
39
}
40
-
/** This (curried) function takes a List[A] and returns a function that takes and Monoid[A] and returns an A which represent the combined value of applying the monoid operation sucessively between each A pair in the list. Making the parameter m implicit here means we only have to provide the xs parameter since once we are given a List[A] we know what type A actually is and therefore what type Monoid[A] is. We can then implicitly find which ever val or object in the current scope also has that type and use that without needing to specify it explicitly. */
40
+
/** This method takes a List[A] returns an A which represent the combined value of applying the monoid operation successively across the whole list. Making the parameter m implicit here means we only have to provide the xs parameter at the call site, since if we have a List[A] we know what type A actually is and therefore what type Monoid[A] is needed. We can then implicitly find whichever val or object in the current scope also has that type and use that without needing to specify it explicitly. */
41
41
def sum[A](xs: List[A])(implicit m: Monoid[A]): A =
42
42
if (xs.isEmpty) m.unit
43
43
else m.add(xs.head, sum(xs.tail))
44
44
45
-
/** Here we call sum with only the first of its (curried) parameters, xs provided. In the first case we pass in a List[Int] and in the second case we pass in a List[String]. Since the second parameter of sum, m, is implicit its value is looked up in the current scope, based on the type of monoid required in each case, meaning both expressions can be fully evaluated. */
45
+
/** Here we call sum twice, with only one parameter each time. Since the second parameter of sum, m, is implicit its value is looked up in the current scope, based on the type of monoid required in each case, meaning both expressions can be fully evaluated. */
0 commit comments