Created
June 8, 2015 22:06
-
-
Save missingfaktor/8c6be26b44fba5c43502 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait Reference | |
| trait Database extends Reference | |
| trait REST extends Reference | |
| trait NoRef extends Reference | |
| trait RefType[A, R <: Reference] { | |
| type Repr | |
| } | |
| trait Customer | |
| trait Product | |
| object RefType { | |
| implicit val customerDatabaseRefType = instance[Customer, Database, Int] | |
| implicit val customerRestRefType = instance[Customer, REST, URI] | |
| implicit val productDatabaseRefType = instance[Product, Database, Int] | |
| implicit val productRestRefType = instance[Product, REST, URI] | |
| implicit def aNoRefType[A] = instance[A, NoRef, A] | |
| def instance[A, R <: Reference, _Repr]: RefType[A, R] = new RefType[A, R] { | |
| type Repr = _Repr | |
| } | |
| } | |
| trait Order { | |
| type R <: Reference | |
| val customerRef: RefType[Customer, R] | |
| val productRef: RefType[Product, R] | |
| def id: Int | |
| def customer: customerRef.Repr | |
| def product: Seq[productRef.Repr] | |
| } | |
| object Order { | |
| def apply[_R <: Reference](_id: Int)(implicit _cR: RefType[Customer, _R], _pR: RefType[Product, _R]) = new { | |
| def apply(_customer: _cR.Repr, _product: Seq[_pR.Repr]) = { | |
| new Order { | |
| type R = _R | |
| val customerRef = _cR | |
| val productRef = _pR | |
| def id = _id | |
| def customer = _customer.asInstanceOf[customerRef.Repr] | |
| def product = _product.asInstanceOf[Seq[productRef.Repr]] | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment