Created
July 30, 2016 15:28
Revisions
-
bistaumanga created this gist
Jul 30, 2016 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,37 @@ import simulacrum.{op, typeclass} import scala.language.experimental.macros object MyOps extends machinist.Ops{ override def operatorNames: Map[String, String] = Map( "$bar$plus$bar" -> "plus" ) } case class MyInt(i: Int) extends AnyVal @typeclass trait Monoid[@specialized(Int, Double) T]{ def zero: T @op("|+|") def plus(lhs: T, rhs: T): T } object Monoid{ trait Ops[A] { def |+|(rhs: A): A = macro MyOps.binop[A, A] } } object MinMonoidMyInt extends Monoid[MyInt]{ override def zero: MyInt = MyInt(Int.MaxValue) override def plus(a: MyInt, b: MyInt): MyInt = MyInt(a.i min b.i) } object MinMonoidInt extends Monoid[Int]{ override def zero: Int = Int.MaxValue override def plus(lhs: Int, rhs: Int): Int = lhs min rhs } object Test extends App{ import Monoid.ops._ implicit val monoid = MinMonoidMyInt println(MyInt(4) |+| MyInt(7) ) }