scala-reflect

How to get to type parameters of a reflect.runtime.universe.Type in scala? [duplicate]

烈酒焚心 提交于 2020-01-15 03:59:26
问题 This question already has answers here : Finding type parameters via reflection in Scala 2.10? (2 answers) Closed 5 years ago . Suppose I get a Type representing List[Int]: > import scala.reflect.runtime.universe > val mirror = universe.runtimeMirror(this.getClass.getClassLoader) mirror: reflect.runtime.universe.Mirror = JavaMirror with ... > class X{ def getList():List[Int] = null } defined class X > val method = mirror. classSymbol(classOf[X]). toType. declarations. filter{_.isMethod}. map{

In scala 2.12, why none of the TypeTag created in runtime is serializable?

独自空忆成欢 提交于 2020-01-14 05:46:13
问题 The bounty expires in 2 days . Answers to this question are eligible for a +100 reputation bounty. tribbloid wants to draw more attention to this question. I'm seeking a method to create a serializable TypeTag without using compile time tools (completely relying on runtime). This is a basic feature for all reflective language. The answer in this post proposed a few methods: In Scala, how to create a TypeTag from a type that is serializable? NONE OF THEM WORKED: package com.tribbloids.spike

In Scala, how to create a TypeTag from a type that is serializable? [duplicate]

别来无恙 提交于 2020-01-02 03:39:12
问题 This question already has answers here : How to create a TypeTag manually? (4 answers) Closed 9 days ago . In Scala reflection, the TypeTag can usually be constructed from a Type using a TypeCreator: object TypeUtils { import ScalaReflection.universe._ def createTypeTag[T]( tpe: Type, mirror: reflect.api.Mirror[reflect.runtime.universe.type] ): TypeTag[T] = { TypeTag.apply( mirror, NaiveTypeCreator(tpe) ) } case class NaiveTypeCreator(tpe: Type) extends reflect.api.TypeCreator { def apply[U <

In Scala reflection, why reflection function on TypeTag still has type erasure?

旧城冷巷雨未停 提交于 2019-12-25 07:26:09
问题 Considering the following scala program: val arr: Seq[String] = Seq("abc", "def") val cls = arr.head.getClass println(cls) val ttg: TypeTag[Seq[String]] = typeOf[Seq[String]] val fns = ttg.tpe .members val fn = fns .filter(_.name.toString == "head") .head // Unsafely access it for now, use Option and map under normal conditions .asMethod // Coerce to a method symbol val fnTp = fn.returnType println(fnTp) val fnCls = ttg.mirror.runtimeClass(fnTp) assert(fnTp == cls) Since TypeTag has both Seq

How to get the runtime value of parameter passed to a Scala macro?

点点圈 提交于 2019-12-23 10:19:10
问题 I have an ostensibly simple macro problem that I’ve been banging my head against for a few hours, with no luck. Perhaps someone with more experience can help. I have the following macro: import scala.language.experimental.macros import scala.reflect.macros.blackbox.Context object MacroObject { def run(s: String): Unit = macro runImpl def runImpl(c: Context)(s: c.Tree): c.Tree = { import c.universe._ println(s) // <-- I need the macro to know the value of s at compile time q"()" } } The

Scala: How to instantiate an interpreter that inherits the current context?

纵饮孤独 提交于 2019-12-23 03:14:16
问题 In a Scala code, I'd like to create an interpreter that will evaluate some strings which are Scala code, e.g., using ScriptEngine. But I'd like to pass the current variable and type definitions to it so that the code in the strings can use them, as if the new interpreter is forked from the current interpreter. With ScriptEngine I could use use the "put" method to put bindings into it, but this needs to be explicit and for each variable. And, there's no way to pass a class definition, or a

Get fully qualified method name in scala macros

故事扮演 提交于 2019-12-23 01:24:09
问题 I use Scala macros and match Apply and I would like to get fully qualified name of the function which is called. Examples: println("") -> scala.Predef.println scala.Predef.println("") -> scala.Predef.println class Abc { def met(): Unit = ??? } case class X { def met(): Unit = ??? def abc(): Abc = ??? } val a = new Abc() val x = new Abc() a.met() -> Abc.met new Abc().met() -> Abc.met X() -> X.apply X().met() -> X.met x.met() -> X.met x.abc.met() -> Abc.met On the left side is what I have in

In Scala Reflection, How to get generic type parameter of a concrete subclass?

末鹿安然 提交于 2019-12-18 05:56:16
问题 Assuming that I have a Generic superclass: class GenericExample[T]( a: String, b: T ) { def fn(i: T): T = b } and a concrete subclass: case class Example( a: String, b: Int ) extends GenericExample[Int](a, b) I want to get the type parameter of function "fn" by scala reflection, so I select and filter through its members: import ScalaReflection.universe._ val baseType = typeTag[Example] val member = baseType .tpe .member(methodName: TermName) .asTerm .alternatives .map(_.asMethod) .head val

Strange type mismatch with a macro: found: singleton type with underlying type A, required: A

主宰稳场 提交于 2019-12-12 11:34:30
问题 I have class Foo[A] { def foo[B](x: A, y: B) = y } class Bar[A] extends Foo[A] { override def foo[B](x: A, y: B) = superCall } where superCall whitebox macro should expand to super.foo[B](x, y) , and that's what -Ymacro-debug-lite shows. The problem is that it fails to compile with following error: [error] /home/aromanov/IdeaProjects/scala-dry/src/test/scala/com/github/alexeyr/scaladry/SuperTests.scala:80: type mismatch; [error] found : y.type (with underlying type B) [error] required: B

Get case class annotations with reflection?

五迷三道 提交于 2019-12-11 07:15:48
问题 I have a case class like: case class Foo (@Annotation bar: String) I'd like to be able to get access to that annotation and any of its information that is stored I can get the case accessors using scala reflection (using 2.11.8) with val caseAccessors = universe.typeTag[T]. tpe. decls. filter(_.isMethod). map(_.asMethod). filter(_.isCaseAccessor) But when I try and access .annotations on them there is nothing. I realize that the annotation is technically on the constructor parameter, but how