Questions tagged [scala-reflect]

In Scala 2.10, a new reflection library was introduced to add a more powerful toolkit of general reflective capabilities to Scala. Along with full-featured runtime reflection for Scala types and generics, Scala 2.10 also ships with compile-time reflection capabilities, in the form of macros, as well as the ability to reify Scala expressions into abstract syntax trees.

278 questions
2
votes
2 answers

java.lang.NoSuchMethodException: scala.collection.immutable.$colon$colon

I am trying to call a function using a variable as String type dynamically i.e. variable will contain a function name as String. So, I need to call a function using that variable. So, I am using Scala Reflection. It is working if the function is…
Tarun Khaneja
  • 451
  • 11
  • 23
2
votes
1 answer

What does scala reflection API Type.dealias do?

I have a simple code, case class Person(name: String, age:Int) import scala.reflect.runtime.universe._ val t1 = typeOf[Person] val t2 = t1.dealias println(t1 == t2) It outputs true, so I want to ask what is Type.dealias used for? When should I use…
Tom
  • 5,848
  • 12
  • 44
  • 104
2
votes
1 answer

Compilation error "not found value ..." after macros expansion

I'm a newcomer to scala-macros. I'm writing an automated JSON writer/reader for the InfluxDB client. The reader looks like: trait InfluxReader[T] { def read(js: JsArray): T } InfluxFormatter: object InfluxFormatter { /** * Generate…
fsanaulla
  • 51
  • 1
  • 5
2
votes
1 answer

How is 'classOf' expression represented in scala AST

I'm new to the Scala AST concepts. I need to traverse a Scala code tree that contains an expression such as: classOf[org.apache.commons.lang3.ArrayUtils] I need to be able to identify this case when pattern matching on…
Natan
  • 1,944
  • 1
  • 11
  • 16
2
votes
1 answer

Did Trees$Literal (reflection) move in Scala 2.13.0-M3?

I'm getting the following 2.13.0-M3 compiler error: type Trees$Literal is not a member of package scala.reflect.internal This compiled fine under 2.11 and 2.12. In 2.13.0-M3 I get the error in the title. Did this change? An example full line of code…
Greg
  • 10,696
  • 22
  • 68
  • 98
2
votes
0 answers

Case class reflection in scala

I have two questions. First off, this is my reflection code for instantiating a class constructor via reflection: case class RowToScalaConverter[T: TypeTag]() { private val tpe: _root_.scala.reflect.runtime.universe.Type = typeOf[T] …
jmc
  • 51
  • 1
  • 6
2
votes
1 answer

Accessing a case class annotation

case class FieldName(field: String) extends scala.annotation.StaticAnnotation @FieldName("foo") trait Foo import scala.reflect.runtime.universe._ symbolOf[Foo].annotations.head // ann: Annotation = FieldName("type") How do I access the annotation…
Reactormonk
  • 21,472
  • 14
  • 74
  • 123
2
votes
1 answer

Is the Symbol a case object?

{ sealed trait Sealed; case object Foo extends Sealed; case class Bar(s: String) extends Sealed; trait Baz extends Sealed } import scala.reflect.runtime.universe._ val List(bar, baz, foo) = symbolOf[Sealed].asClass.knownDirectSubclasses.toList I've…
Reactormonk
  • 21,472
  • 14
  • 74
  • 123
2
votes
0 answers

How does one use Scala reflection to typecheck code in a package parsed from ToolBox?

The following trivial example (run in Scala 2.11.8): import scala.tools.reflect.ToolBox import scala.reflect.runtime.currentMirror object Main extends App { val toolbox = currentMirror.mkToolBox() val tree = toolbox.parse { """ …
Brian Kent
  • 3,754
  • 1
  • 26
  • 31
2
votes
1 answer

How to write a Scala macro that evaluates to a Tree similar to "reify"

I want to write a macro that captures a program snippet and makes it available as a Tree at runtime. Basically, I want the functionality of reify but embed it in a different syntax. I want to call apply on a Workload companion object, supply some…
Johannes Luong
  • 574
  • 5
  • 19
2
votes
1 answer

In Scala, What is the difference between universe.TypeTag, TypeRef, and Type?

There are 3 different classes all bounded to a universe: TypeTag, TypeRef, and Type. Why do we need all 3? If I only have a Type, how do I convert it to a TypeTag or TypeRef? E.g. I have obtain a common super type through a Scala reflection API…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
2
votes
1 answer

Getting proper type constructor parameters for a "refined" type

I'm having problems comparing the 'compatibility' between two types using reflection (actually I'm writing a macro). For example, I want to permit Vector[Int] === List[Int]. Now I know the general approach. But the problem is I cannot get the type…
0__
  • 66,707
  • 21
  • 171
  • 266
2
votes
1 answer

Finding whether a symbol is visible or shadowed at the point of macro expansion

I have a Symbol and want to check if it's visible at the point of macro expansion or shadowed (e.g. by a local variable), so that splicing the symbol's name in the quasiquote doesn't refer to something else. Looking at…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
2
votes
1 answer

Get field value of a companion object from TypeTag[T]

I have a quite rare use case where a trait is being implemented by 3rd party (think of a plugin architecture) and I want to get a field of each trait's companion object. A simple trait implementation looks like this: trait Plugin { val ID:…
yarinbenado
  • 179
  • 4
  • 11
2
votes
0 answers

How to find parent implicit parameter with scala-reflect?

I have two classes: class X[A](implicit ord: Ordering[A]) // + other irrelevant parameters class Y[A: Ordering] extends X[A] Given a Symbol corresponding to Y's implicit constructor parameter, I want to find the symbol for the parent class implicit…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487