Created
July 30, 2016 15:28
-
-
Save bistaumanga/33402a539d30a979cc80d72b33e53910 to your computer and use it in GitHub Desktop.
simulacrum + machinist
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 characters
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) ) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Getting following error: