Skip to content

Commit 6ca990f

Browse files
authored
Merge pull request #45 from jonboiser/patch-1
Implicits Chapter: Edits to docstrings, plus one code change
2 parents aed0021 + 9e0d2c4 commit 6ca990f

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/main/scala/stdlib/Implicits.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import scala.language.implicitConversions
88
*/
99
object Implicits extends FlatSpec with Matchers with org.scalaexercises.definitions.Section {
1010

11-
/** The actual arguments that are eligible to be passed to an implicit parameter fall into two categories: * First, eligible are all identifiers x that can be accessed at the point of the method call without a prefix and that denote an implicit definition or an implicit parameter. * Second, eligible are also all members of companion modules of the implicit parameter's type that are labeled implicit.
11+
/** The actual arguments that are eligible to be passed to an implicit parameter fall into two categories:
1212
*
13-
* 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.
13+
* - First, eligible are all identifiers x that can be accessed at the point of the method call without a prefix and that denote an implicit definition or an implicit parameter.
14+
* - Second, eligible are also all members of companion modules of the implicit parameter's type that are labeled implicit.
15+
*
16+
* 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.
1417
*
1518
* {{{
1619
* abstract class SemiGroup[A] {
@@ -45,7 +48,7 @@ object Implicits extends FlatSpec with Matchers with org.scalaexercises.definiti
4548
*
4649
* Implicits wrap around existing classes to provide extra functionality. This is similar to *monkey patching* in **Ruby**, and *Meta-Programming* in **Groovy**.
4750
*
48-
* Creating a method isOdd for Int, which doesn't exist:
51+
* Creating a method `isOdd` for `Int`, which doesn't exist:
4952
*/
5053
def implicitsParametersImplicits(res0: Boolean, res1: Boolean) {
5154
class KoanIntWrapper(val original: Int) {
@@ -80,7 +83,7 @@ object Implicits extends FlatSpec with Matchers with org.scalaexercises.definiti
8083

8184
/** convertTypeImplicits
8285
*
83-
* Implicits can be used to automatically convert one type to another
86+
* Implicits can be used to automatically convert a value's type to another:
8487
*/
8588
def convertTypeImplicits(res0: Boolean, res1: Boolean, res2: Boolean, res3: Boolean, res4: Boolean) {
8689
import java.math.BigInteger
@@ -97,9 +100,9 @@ object Implicits extends FlatSpec with Matchers with org.scalaexercises.definiti
97100
add(3, 6).intValue == 9 should be(res4)
98101
}
99102

100-
/** Implicits can be used to declare a value to be provided as a default as long as an implicit value is set with in the scope. These are called implicit function parameters:
103+
/** Implicits can be used to declare a value to be provided as a default as long as an implicit value is set with in the scope. These are called Implicit Function Parameters:
101104
*/
102-
def asDefaultImplicits(res0: Float) {
105+
def asDefaultImplicits(res0: BigDecimal) {
103106
def howMuchCanIMake_?(hours: Int)(implicit dollarsPerHour: BigDecimal) = dollarsPerHour * hours
104107

105108
implicit val hourlyRate = BigDecimal(34.00)
@@ -119,7 +122,7 @@ object Implicits extends FlatSpec with Matchers with org.scalaexercises.definiti
119122
howMuchCanIMake_?(30) should be(res0)
120123
}
121124

122-
/** Default arguments though are preferred to Implicit Function Parameters
125+
/** Default arguments, though, are preferred to Implicit Function Parameters:
123126
*/
124127
def defaultArgumentsImplicits(res0: String, res1: String) {
125128
def howMuchCanIMake_?(hours: Int, amount: BigDecimal = 34, currencyName: String = "Dollars") =

src/test/scala/stdlib/ImplicitsSpec.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ class ImplicitsSpec extends Spec with Checkers {
3535
}
3636

3737
def `implicits for default parameters` = {
38-
val fstAnswer: Float = 1020.0f
39-
val sndAnswer: Float = 9025.0f
38+
val fstAnswer: BigDecimal = 1020.0f
4039

4140
check(
4241
Test.testSuccess(

0 commit comments

Comments
 (0)