Skip to content

Instantly share code, notes, and snippets.

@mcalavera81
Created December 22, 2015 11:23
Show Gist options
  • Save mcalavera81/cb8fbb134adfefb0bc23 to your computer and use it in GitHub Desktop.
Save mcalavera81/cb8fbb134adfefb0bc23 to your computer and use it in GitHub Desktop.
trait Combinable[T]{
def combine(a:T,b:T):T
}
object Combinable{
def apply[T](implicit c:Combinable[T]) = c
trait CombinableOps[T]{
def self:T
def combinable: Combinable[T]
def combine(other: T) = combinable.combine(self,other)
}
object ops{
implicit def toCombinableOps[T](t:T)(implicit c:Combinable[T])={
new CombinableOps[T] {
override def self: T = t
override def combinable: Combinable[T] = c
}
}
}
implicit object StringCombinable extends Combinable[String]{
override def combine(a: String, b: String): String =
a+b
}
}
Combinable[String].combine("aa","bb")
import Combinable.ops._
"aa" combine "bb" combine "cc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment