如何对DefDef进行类型检查 [英] How to Typecheck a DefDef

查看:80
本文介绍了如何对DefDef进行类型检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在注解宏中,我正在枚举类的成员,并希望找到的方法的类型.

Within an annotation Macro, I'm enumerating members of a class and want the types of the methods that I find.

所以我很高兴地遍历该类的body,并收集了所有的DefDef成员.

So I happily iterate over the body of the class, and collect all the DefDef members.

...我无法进行类型检查.

... which I can't typecheck.

对于每个DefDef,我都尝试将其包装在Expr中并使用actualType.我已经尝试过将其复制并移植到一个临时类中(通过准引用).我已经尝试了我能想到的其他所有方法:)

For each DefDef I've tried wrapping it in an Expr and using actualType. I've tried duplicating the thing and transplanting it into an ad-hoc class (via quasiquotes). I've tried everything else I can think of :)

根据所使用的技术,我能得到的最好的结果是NoTypeAny.我遇到的最糟糕的情况是对我抛出异常.

The best I can get is either NoType or Any, depending on the technique used. The worst I get is to have an exception thrown at me.

这些是简单的方法,格式为def foo(i: String) = i,因此需要推断返回类型,但是不需要外部信息.这里没有抽象类型,类型参数或类的其他成员.我想稍后再处理更高级的案例,但希望首先使用这些琐碎的示例.

These are simple methods, of the form def foo(i: String) = i, so the return type needs to be inferred, but there's no external information required. There are no abstract types, or type params, or other members of the class involved here. I'd like to handle more advanced cases later, but want to have these trivial examples working first.

在插件中,这很简单.我只是对整个单元进行类型检查,并抑制错误,并通过符号获得所需的值,然后重置树属性以进行后续处理.作为宏...我很困惑.

In a plugin, this would be simple. I'd just typecheck the entire unit with errors suppressed and get at what I want through the symbols, then reset the tree attributes for subsequent processing. As a macro... I'm stumped.

我想念什么?

推荐答案

在宏中是相同的.您可以调用c.typeCheck来代替插件中的typed,但必须小心不要陷入陷阱( https://github.com/scalamacros/paradise/issues/1 )应该在2.10.5和2.11.0中进行了修复. c.typeCheck成功返回后,您就可以访问该符号并进行所有常规操作.

In a macro it's the same. Instead of typed as in plugins, you call c.typeCheck, but have to be careful not to fall into a trap (https://github.com/scalamacros/paradise/issues/1) that is supposed to be fixed in 2.10.5 and 2.11.0. After a successful return from a c.typeCheck, you can get access to the symbol and do all the usual stuff.

这篇关于如何对DefDef进行类型检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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