-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplicits.scala
More file actions
46 lines (39 loc) · 1.1 KB
/
implicits.scala
File metadata and controls
46 lines (39 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.dreadedsoftware
object implicits extends App{
import scala.language.implicitConversions
//nice for post hoc polymorphism
object conversions{
implicit def intToString(int: Int): String = int.toString()
def apply(): Unit = {
assert(7 == numChars(1000000))
}
def numChars(string: String): Int = string.length()
}
//nice for ad-hoc polymorphism
object arguments{
implicit def lastName: String = "Stewart-Baxter"
def apply(): Unit = {
assert("Marjorie Stewart-Baxter" == name("Marjorie"))
}
def name(given: String)(implicit sur: String) = {
s"$given $sur"
}
}
//nice for extension decorating
object wrappers{
implicit class Namer(val str: String) extends AnyVal{
def given: String = str
def sur: String = "Stewart-Baxter"
def full: String = s"$given $sur"
}
def apply(): Unit = {
val name = "Marjorie"
assert(name == name.given)
assert("Stewart-Baxter" == name.sur)
assert(s"$name Stewart-Baxter" == name.full)
}
}
conversions()
arguments()
wrappers()
}