Skip to content

Instantly share code, notes, and snippets.

@ryanonsrc
Created February 18, 2015 23:27
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 ryanonsrc/e3a07fe146d58501283b to your computer and use it in GitHub Desktop.
Save ryanonsrc/e3a07fe146d58501283b to your computer and use it in GitHub Desktop.
trait QId; trait QSection; trait QProjectType; trait QName
trait QFriendlyName; trait QDescription; trait QCode; trait QAnswers
case class Answer(text: String)
object QuestionTypes {
type FormQuestionId = Long @@ QId
type Section = String @@ QSection
type ProjectType = String @@ QProjectType
type Name = String @@ QName
type FriendlyName = String @@ QFriendlyName
type Description = String @@ QDescription
type Code = Option[Int] @@ QCode
type Answers = Seq[Answer] @@ QAnswers
def extract[A] = new {
def from[C, Repr <: HList](c: C)(implicit
gen: Generic.Aux[C, Repr],
sel: Selector[Repr, A]
) = sel(gen.to(c))
}
}
import QuestionTypes._
case class Question (
id: FormQuestionId,
section: Section,
projectType: ProjectType,
name: Name,
friendlyName: FriendlyName,
description: Description,
code: Code,
answers: Answers
)
// given a question of type Question
import QuestionTypes._
val x = extract[Answers].from(question)
// x is of type sel.type#Out, and I would like to get the answers array instead
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment