Search code examples
scalascalameta

how to append a implicit params use scala meta annotation?


for a example,append a implicit param id for func method:
Before

def func(p1: String) = { println("hi")}

After

@Param
def func(p1: String)(implicit id: String = "default") = { println("hi")}

Is scala meta able do this?


Solution

  • I got how to do this:

    //create Param  
    val impParam = Term.Param(Nil, Term.Name("id"), Some(Type.Name("String")), Some(Term.Name("default")))
    //append to existing params seq.`defn` is this method meta object
    val appendImpParam = defn.paramss :+ impParam
    

    Notice: you'd better check if the method had exist implicit params.