-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathGeneric.scala
57 lines (33 loc) · 1.73 KB
/
Generic.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
50
51
52
53
54
55
56
57
package learnshapeless
import Data._
import shapeless._
case class PersonDetails(firstName: String, surname: String)
case class Asset(description: String, value: BigDecimal, categoryCode: String, verified: Boolean)
object Ch04_GenericRepresentation extends App {
/** Generic provides a powerful set of macros that use compile-time reflection to convert between class hierachies
* and a generic form based on HLists (aka "products") and Coproducts (aka "sums")
* */
/** Lets start by creating a relatively complex domain model for a load agreement */
def eg_james = PersonDetails("James", "Wong")
def eg_genPersonDetails = Generic[PersonDetails]
def eg_genericJames: String :: String :: HNil = eg_genPersonDetails.to(eg_james)
def eg_country: Country = Australia
def eg_genCountry = Generic[Country]
def eg_genericCountry: Australia.type :+: England.type :+: Germany.type :+: CNil = eg_genCountry.to(eg_country)
/** Product exercise:
1. Assign a Generic typeclass for Asset to `ex_genAsset`.
2. Use it to convert `eg_asset` to generic form. Specify its explicit type.
3. Uncomment and complete the assertion by filling the expected `head` value **/
def eg_asset = Asset("2012 Toyota Camry", BigDecimal(19500.0), "MOT2", false)
def ex_genAsset = ???
def ex_genericAsset = ???
//assertEquals(???, ex_genericAsset.head)
/** Coproduct exercise:
* 1. Assign a Generic typeclass for State to `ex_genState`.
* 2. Use it to convert `eg_state` to generic form. Specify its explicit type.
* 3. Uncomment and complete the assertion by filling the expected `head` value*/
def eg_state: State = Victoria
def ex_genState = ???
def ex_genericState = ???
//assertEquals(None, ex_genericState.head)
}