-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTypedMapTest.scala
49 lines (41 loc) · 1.08 KB
/
TypedMapTest.scala
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
47
48
49
final class MyTypeMap extends TypeMap
{
// define allowed mappings
implicit val StrToBool = new TypeMapping[String, Boolean] {}
implicit val StrToInt = new TypeMapping[String, Int] {}
implicit val IntToStr = new TypeMapping[Int, String] {}
implicit val LStrToStr = new TypeMapping[List[String], Int] {}
implicit val LIntToStr = new TypeMapping[List[Int], Int] {}
}
object TypedMapTest
{
val m = new MyTypeMap
import m._
m("a") = 3
m("a") = true
// compilation error
//m("a") = "string"
m(5) = "asdf"
m(17) = "jkl"
m( List[String]() ) = 9
m( List[Int]() ) = 4
m( 5 :: Nil ) = 7
m( "xt" :: Nil ) = 7
// compilation error
//m( 'z' :: Nil) = 0
def run()
{
assert( m[String, Int]("a") == 3 )
assert( m[String, Boolean]("a") == true )
assert( m(5) == "asdf" )
assert( m(17) == "jkl" )
assert( m( List[String]() ) == 9 )
assert( m( List[Int]() ) == 4 )
assert( m( 5 :: Nil ) == 7 )
assert( m( "xt" :: Nil ) == 7 )
assert( m.getOr("a", 5) == 3)
assert( m.getOr("b", 5) == 5)
assert( m.getOr("a", false) == true)
assert( m.getOr("b", false) == false)
}
}