Skip to content

Instantly share code, notes, and snippets.

@bistaumanga
Created July 30, 2016 15:28
Show Gist options
  • Save bistaumanga/33402a539d30a979cc80d72b33e53910 to your computer and use it in GitHub Desktop.
Save bistaumanga/33402a539d30a979cc80d72b33e53910 to your computer and use it in GitHub Desktop.
simulacrum + machinist
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) )
}
@bistaumanga
Copy link
Author

Getting following error:

Error:(8, 2) Ops is already defined as trait Ops
@typeclass trait Monoid[@specialized(Int, Double) T]{

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