user1187135
user1187135

Reputation:

How to use cq in quasi quote to return the matched pattern

I am trying to write this case authorDao: AuthorDao => authorDao so that it returns the subclass of Dao itself.

When I use this quasi quote:

val daoType = TypeName(daoName)
val caseTerm = TermName(daoName.toLowerCase)

cases.append(cq"$caseTerm: $daoType=> $caseTerm")

It generates this

case (authordao @ ((_): AuthorDao)) => authordao

And if I do this

cases.append(cq"${q"$caseTerm: $daoType"} => $caseTerm")

It does this

case ((authordao): AuthorDao) => authordao

Both are produce compile errors

Upvotes: 0

Views: 82

Answers (1)

user1187135
user1187135

Reputation:

After some googling, I found the answer here:

Scala multiple type pattern matching

Basically

case authordao: AuthorDao => authordao is equivalent to this

case authordao @ AuthorDao(_) => authordao

So the final code is this

      val daoTerm = TermName(daoName)
      val caseType = TypeName(daoName.toLowerCase())
      val caseExpr = TermName(daoName.toLowerCase)

      cases.append(cq"$caseType @ $daoTerm(_) => $caseExpr")

Upvotes: 1

Related Questions