Skip to content

Commit 9a3326e

Browse files
authored
Merge pull request #558 from axiomsofchoice/master
Provided commentary for the implicit parameter example in the tutorials.
2 parents baa4104 + 82e57d6 commit 9a3326e

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

tutorials/tour/implicit-parameters.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ The actual arguments that are eligible to be passed to an implicit parameter fal
1919

2020
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.
2121

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. */
2223
abstract class SemiGroup[A] {
2324
def add(x: A, y: A): A
2425
}
26+
/** A monoid is a semigroup with a distinguished element of A, called unit, that when combined with any other element of A returns that other element again. */
2527
abstract class Monoid[A] extends SemiGroup[A] {
2628
def unit: A
2729
}
2830
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. */
2932
implicit object StringMonoid extends Monoid[String] {
3033
def add(x: String, y: String): String = x concat y
3134
def unit: String = ""
@@ -34,12 +37,14 @@ In the following example we define a method `sum` which computes the sum of a li
3437
def add(x: Int, y: Int): Int = x + y
3538
def unit: Int = 0
3639
}
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. */
3741
def sum[A](xs: List[A])(implicit m: Monoid[A]): A =
3842
if (xs.isEmpty) m.unit
3943
else m.add(xs.head, sum(xs.tail))
4044

41-
println(sum(List(1, 2, 3)))
42-
println(sum(List("a", "b", "c")))
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. */
46+
println(sum(List(1, 2, 3))) // uses IntMonoid implicitly
47+
println(sum(List("a", "b", "c"))) // uses StringMonoid implicitly
4348
}
4449

4550
Here is the output of the Scala program:

0 commit comments

Comments
 (0)