Scala宏Liftable具有前向引用

时间:2018-03-17 11:34:05

标签: scala macros

我非常粗略地拥有以下代码:

object MyObj {
  def callWithParams(params: List[Param]): String = "some string"
}

sealed trait Param
case class Single(id: Int) extends Param
case class Group(id: Int, subParams: List[Param]) extends Param

def buildMyParams(): List[Param] = List(Single(1), Group(2, List(Group(3, Single(4))))

def macroImpl(c: blackbox.Context): c.Expr[String] = {
  import c.universe._

  // TODO: need a implicit Lift[Param] implementation here

  val myParams = buildMyParams()
  c.Expr[String](q"MyObj.callWithParams($myParams)")
}

我对隐式List[Param]的尝试是:

implicit val lift = Liftable[Param]({
  case s: Single => q"Single(${s.id})"
  case g: Group => q"Group(${g.id}, ${g.subParams})"
})

由于Liftable因为Group而尝试解除Group.subParams$.ajax({ Type: 'GET', url: 'api/controller1', dataType: 'JSON', success: function (data) { var htmlString = ''; $(data).each(function (index, data) { var myDate = new Date(data.date); htmlString += '<h3>' + String(myDate) + '</h3><div>' + data.ID + "<br/>" + + data.stringvalue + '</div>'; }); $('#accordion').html(htmlString).accordion({ collapsible: true }); 中的转发引用,因此无法编译。

如何解决此问题?

1 个答案:

答案 0 :(得分:2)

将升力声明为def而不是val足以解决这个问题:

implicit def lift = Liftable[Param]({
  case s: Single => q"Single(${s.id})"
  case g: Group => q"Group(${g.id}, ${g.subParams})"
})