Skip to content

Instantly share code, notes, and snippets.

@vil1
Created August 25, 2015 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vil1/332d3c959aff8e93c0e4 to your computer and use it in GitHub Desktop.
Save vil1/332d3c959aff8e93c0e4 to your computer and use it in GitHub Desktop.
import shapeless._
object definition {
case class Property[A](a: A)
case class Embed[A](a: A)
case class Hidden[A](a: A)
def Prop[X] = Property[X] _
def Emb[X] = Embed[X] _
def Hid[X] = Hidden[X] _
trait Representation[A] {
type Repr <: HList
val repr: Repr
}
object Representation {
type Aux[A, R] = Representation[A]{ type Repr = R }
def apply[A, R <: HList](genA: Generic[A], r: R)(implicit typeAlignment: TypeAlignment[genA.Repr, R]): Aux[A, R] = new Representation[A] {
type Repr = R
val repr = r
}
}
case class TypeAlignment[A, B]()
object TypeAlignment {
implicit def hnilTypeAlign2: TypeAlignment[HNil, HNil] = new TypeAlignment[HNil, HNil]()
implicit def propertyRightAlign[A]: TypeAlignment[A, A => Property[A]] = new TypeAlignment[A, A => Property[A]]()
implicit def embedRightAlign[A]: TypeAlignment[A, A => Embed[A]] = new TypeAlignment[A, A => Embed[A]]()
implicit def hiddenRightAlign[A]: TypeAlignment[A, A => Hidden[A]] = new TypeAlignment[A, A => Hidden[A]]()
implicit def hlistTypeAlign[A, B, TA <: HList, TB <: HList](implicit headAlignment: TypeAlignment[A, B], tailAlignment: TypeAlignment[TA, TB]): TypeAlignment[A :: TA, B :: TB] = new TypeAlignment[A :: TA, B :: TB]()
}
}
import shapeless._
object demo {
import definition._
type ACL = String
case class Address(city: String, street: String, zipcode: String)
case class User(name: String, age: Option[Int], address: Address, acl : ACL)
val userRepr = Prop[String] :: Prop[Option[Int]] :: Emb[Address] :: Hid[ACL] :: HNil
val bogusRepr = Prop[String] :: Prop[Int] :: Emb[Address] :: Hid[ACL] :: HNil
/*def makeUserRepr(implicit gen: Generic[User]) = {
val genUser = Generic[User]
Representation(genUser, userRepr)
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment