Generate functions at macros expansion time

陌路散爱 提交于 2019-12-04 09:43:28

What you're trying to do is easily achievable with implicit conversions, so you don't really need macros:

case class C[T] (t: T)

object C { //we define implicit conversion in companion object
  implicit def conversion[T](c: C[T]): T = c.t
}

import scala.language.implicitConversions
import C._

val c1 = C(1)
assert(c1 + 1 == 2) //ok

val c2 = C(false)
assert(!c2 && true) //ok

Using implicit conversions means, that whenever compiler would notice, that types don't match, it would try to implicitly convert value applying implicit function.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!