how to print variable name and value using a scala macro? -


I'm sure that the following macro is another great way to write that prints the name and value of a variable: / P>

  def mprintx (c: context) (linecode: c.Expr [any]): c.Expr [unit] = {import c.universe._walname = (c.enclosingImpl match { Case ClassDef (mods, name, tparams, impl) => c.universe.reify (c.literal (name.toString) .splice) case ModuleDef (mods, name, impl) => c.universe.reify (C. Lexical (name.toString) .splice) case _ = & gt; c.abort (c.enclosingPosition, "NoEnclosingClass")}) toString Match {case r_name (n) = & gt; N case _ = & gt; "Unknown?" } Val Message = linecode.tree.productIterator.toList.last.toString.replaceAll ("Scala. * \\]", "") .replaceAll (namez + "\\. This \\." "") .replaceAll ( Think of something like "list", "") (myPrintDln (c.Expr [string] (verbatim (continuously (message)) splice. "---"> + linecode.splice)}} Def myPrintIt (linecode: any) = Macro MPrintux  

is called by the following program:

  Object Zabi2 app {val l = "zab" val Kol = 345 var zub = List ("2", 89) Wal Zubby = List (zub, L, Cole) printIt (L) printIt (L, Cole, (L, zub), zubi)}  

print:

< Pre> L ---> Zab (L, Cole, (L, Zab), Zubby) --- & gt;

Thank you in advance for your help.

P>


Comments