如何在scala宏中从带有类型参数的类型中获取类型参数? [英] How do I get type parameter from type with type parameter, inside scala macro?

查看:97
本文介绍了如何在scala宏中从带有类型参数的类型中获取类型参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型(在这种情况下,它表示方法的返回类型),并且它的形式为List[Int](例如).

I have a type (that, in this case, represents return type of a method), and it is of the form List[Int] (for example).

我知道类型携带该信息,因为toString会得出正确的值,但是如何提取该类型?我尝试了.typeSymbol,但是这完全丢失了所有类型信息.

I know that the type carries that information, because toString results in the proper value, but how can I extract that type? I tried .typeSymbol, but that loses all type information altogether.

推荐答案

您可以使用

You can use the TypeRef extractor:

import reflect.runtime.universe._
// or in a macro "c.universe._"

val tpe = weakTypeOf[List[Int]]

// extract type parameters
val TypeRef(_,_, tps) = tpe

// tps has type List[Type]
println(tps.head) // Int

这篇关于如何在scala宏中从带有类型参数的类型中获取类型参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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