Skip to content

Instantly share code, notes, and snippets.

@okram
Last active April 26, 2020 23:43
Show Gist options
  • Save okram/820c9b350146df507276ecf17eff20d9 to your computer and use it in GitHub Desktop.
Save okram/820c9b350146df507276ecf17eff20d9 to your computer and use it in GitHub Desktop.
trait MultOp[O <: Obj] {
this: O =>
def mult(anon: __): this.type = MultOp(anon).exec(this)
def mult(arg: O): this.type = MultOp(arg).exec(this)
final def *(anon: __): this.type = this.mult(anon)
final def *(arg: O): this.type = this.mult(arg)
}
object MultOp {
def apply[O <: Obj](obj: Obj): MultInst[O] = new MultInst[O](obj)
class MultInst[O <: Obj](arg: Obj, q: IntQ = qOne) extends VInst[O, O]((Tokens.mult, List(arg)), q) {
override def q(q: IntQ): this.type = new MultInst[O](arg, q).asInstanceOf[this.type]
override def exec(start: O): O = {
val resolvedArg: Obj = Inst.resolveArg(start, arg)
Try(start match {
case aint: Int => start.clone(value = aint.value * resolvedArg.asInstanceOf[Int].value)
case areal: Real => start.clone(value = areal.value * resolvedArg.asInstanceOf[Real].value)
}).getOrElse(start).via(start, new MultInst(resolvedArg,this.q))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment