如何在Scala 2.11中查找包含源文件的名称 [英] How to find the name of the enclosing source file in Scala 2.11

查看:65
本文介绍了如何在Scala 2.11中查找包含源文件的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译时,如何在scala 2.11中检索当前源文件的名称(编写代码的位置)?

At compile time, how to retrieve the name of the current source file (where the code is written) in scala 2.11?

推荐答案

在REPL中,名称为console,但这表明职位知道其来源.

In the REPL, the name is console, but this shows that a position knows its source.

scala> import scala.language.experimental.macros
import scala.language.experimental.macros

scala> import scala.reflect.macros.whitebox.Context
import scala.reflect.macros.whitebox.Context

scala> def f(c: Context): c.Tree = { import c._,universe._ ; Literal(Constant(c.enclosingPosition.source.file.name)) }
f: (c: scala.reflect.macros.whitebox.Context)c.Tree

scala> def g: String = macro f
defined term macro g: String

scala> g
res0: String = <console>

这篇关于如何在Scala 2.11中查找包含源文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆