Skip to content

Instantly share code, notes, and snippets.

@hisui
Created August 5, 2013 04:28
Show Gist options
  • Save hisui/6153511 to your computer and use it in GitHub Desktop.
Save hisui/6153511 to your computer and use it in GitHub Desktop.
An experiment construction to treat n-tuples generically.
trait GenericTuple[T] {
type Base = T
type Left[A]
type Right[A]
def <<[A](o:Base, a:A): Left[A]
def >>[A](a:A, o:Base):Right[A]
}
object GenericTuple {
implicit def tuple2[A,B] = new GenericTuple[(A,B)] {
def <<[A](o:Base, a:A) = (o._1, o._2, a)
def >>[A](a:A, o:Base) = (a, o._1, o._2)
override type Left[X] = (A,B,X)
override type Right[X] = (X,A,B)
}
implicit def tuple3[A,B,C] = new GenericTuple[(A,B,C)] {
def <<[A](o:Base, a:A) = (o._1, o._2, o._3, a)
def >>[A](a:A, o:Base) = (a, o._1, o._2, o._3)
override type Left[X] = (A,B,C,X)
override type Right[X] = (X,A,B,C)
}
}
implicit class TupleOps[T](tuple:T) {
def <<[A](a:A)(implicit ev:GenericTuple[T]) = ev.<<(tuple, a)
}
import GenericTuple._
println( (("nico", "nico") << 2525 << "hoge")._4 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment