Skip to content

Instantly share code, notes, and snippets.

@jonifreeman
Created September 12, 2013 05:54
Show Gist options
  • Save jonifreeman/6533463 to your computer and use it in GitHub Desktop.
Save jonifreeman/6533463 to your computer and use it in GitHub Desktop.
Explicit type for a Shapeless record.
object TestExplicitRecordType {
import shapeless._, record._, syntax.singleton._
object testF extends Poly1 {
implicit def atFieldType[F, V](implicit wk: shapeless.Witness.Aux[F]) = at[FieldType[F, V]] {
f => wk.value.toString
}
}
// Is there more straighforward way to give an explicit type for a record?
val k1 = Witness("k1")
val k2 = Witness("k2")
type Error = FieldType[k1.T, String] :: FieldType[k2.T, Long] :: HNil
// That seems to work...
val err1 = "k1" ->> "1" :: "k2" ->> 1L :: HNil
val err2: Error = "k1" ->> "1" :: "k2" ->> 1L :: HNil
// ... except. Looks like evidence for Witness.Aux is lost.
testF(err1.head) // OK
testF(err2.head) // could not find implicit value for parameter cse: shapeless.poly.Case[testF.type,shapeless.::[shapeless.record.FieldType[k1.T,String],shapeless.HNil]]
}
@milessabin
Copy link

I think this is now addressed via the new record type literals in shapeless 2.1.0 ... does this resolve the problem for you?

object TestExplicitRecordType {
  import shapeless._, labelled._, record._, syntax.singleton._

  object testF extends Poly1 {
    implicit def atFieldType[F, V](implicit wk: shapeless.Witness.Aux[F]) = at[FieldType[F, V]] {
      f => wk.value.toString
    }
  }

  type Error = Record.`"k1" -> String, "k2" -> Long`.T

  // That seems to work...
  val err1        = "k1" ->> "1" :: "k2" ->> 1L :: HNil
  val err2: Error = "k1" ->> "1" :: "k2" ->> 1L :: HNil

  testF(err1.head)  // OK
  testF(err2.head)  // OK
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment