Questions tagged «structural-typing»

1
从宏获取带有匿名类方法的结构类型
假设我们要编写一个宏,该宏使用一些类型成员或方法定义一个匿名类,然后使用这些方法创建该类的实例,该实例被静态类型化为结构类型,依此类推。在2.10中的宏系统中是可行的。 0,并且类型成员部分非常简单: object MacroExample extends ReflectionUtils { import scala.language.experimental.macros import scala.reflect.macros.Context def foo(name: String): Any = macro foo_impl def foo_impl(c: Context)(name: c.Expr[String]) = { import c.universe._ val Literal(Constant(lit: String)) = name.tree val anon = newTypeName(c.fresh) c.Expr(Block( ClassDef( Modifiers(Flag.FINAL), anon, Nil, Template( Nil, emptyValDef, List( constructor(c.universe), TypeDef(Modifiers(), newTypeName(lit), Nil, TypeTree(typeOf[Int])) ) …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.