Skip to content

Instantly share code, notes, and snippets.

@bvenners
Created December 3, 2013 16:49
Show Gist options
  • Save bvenners/7772698 to your computer and use it in GitHub Desktop.
Save bvenners/7772698 to your computer and use it in GitHub Desktop.
Compile time HList for types
// This was my proof of concept sketch for doing matcher factories with an
// compile-time HList for type constructors instead of code-generated MatcherFactoryN's.
// I just stored regular types here, but I figured it would be straightforward to
// make those type constructors. I never tried doing the implicit recursive unrolling
// that would be required to actually use something like this in ScalaTest, but that
// would be fun to try as an intellectual exercise. For ScalaTest I decided that even
// if I could get that working, it would end up being too "clever" for ScalaTest, so
// I opted for the MatcherFactoryN's that I felt would be simpler for users to understand.
sealed trait MatcherFactory
final case class MFNode[+H, +T <: MatcherFactory]() extends MatcherFactory {
def prepend[J]: MFNode[J, MFNode[H, T]] = MFNode[J, MFNode[H, T]]()
override def toString = "MFNode"
}
trait MFNil extends MatcherFactory { // I don't think I need this trait
def prepend[H]: MFNode[H, MFNil] = MFNode[H, MFNil]()
override def toString = "MFNil"
}
case object MFNil extends MFNil
type A = Int
type B = String
type C = Object
MFNil.prepend[A].prepend[B].prepend[C]
@bvenners
Copy link
Author

bvenners commented Dec 3, 2013

If you paste this into a scala REPL you'll see:

scala> MFNil.prepend[A].prepend[B].prepend[C]
res0: MFNode[C,MFNode[B,MFNode[A,MFNil]]] = MFNode

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