Scala 2.9.x では、 FUNC C プリプロセッサ マクロのように func() が実行される関数の名前を返す func 関数を書きました。Scala2.10 では、仕事をするために例外をスローするよりもエレガントなものを書けるはずだということを理解しています。
どうすればいいですか?よろしくお願いします。
object TestMyLog extends App {
val MatchFunc = """(.+)\(.+""".r
def func(i_level: Int): String = {
val s_rien = "functionNotFound"
try {
throw new Exception()
} catch {
case unknwn => unknwn.getStackTrace.toList.apply(i_level).toString match {
case MatchFunc(funcs) => funcs.split('.').toList.last
case _ => s_rien
}
} finally {
s_rien
}
}
def tracedFunction1 = func(1)
def tracedFunction2 = func(1)
println(tracedFunction1)
assert(tracedFunction1=="tracedFunction1")
println(tracedFunction2)
assert(tracedFunction2=="tracedFunction2")
}