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.
Questions tagged [scala-reflect]
278 questions
0
votes
1 answer
In Scala, is the reflection module ill suited for dealing with runtime, possibly erased types?
Considering a simple use case to extract fields of an object in Java reflection:
val fields = this.getClass.getDeclaredFields
it may return incomplete information if generic type is used, but it is short, fast and can handle most cases.
In Scala…

tribbloid
- 4,026
- 14
- 64
- 103
0
votes
1 answer
Checking equality of types after type checking with scala.tools.reflect.ToolBox
I want to determine whether expressions represented by syntax trees tree1 and tree2 are of the same type. I tried to do so using type checking method from scala.tools.reflect.ToolBox, but it seems to be inconsistent with the actual Scala types. …

oquechy
- 51
- 4
0
votes
1 answer
How to dynamically construct subclass with parameters based on string
Code I have:
abstract class Animal {
def init(animalType: String, jsonBlob: String)
}
class Dog (name: String, colour: String) {
def init(animalType: String, jsonBlob: String) : Unit = {
name = jsonBlob.name
colour =…

user
- 352
- 3
- 13
0
votes
0 answers
Evaluate Spark Inside Scala ToolBox
Can I evaluate spark inside scala toolbox? I need to read something with spark within the toolbox, my code looks like this:
object Something extends SparkScript {
def main(args: Array[String]): Unit = {
val command =
s"""
|import…

afghifari
- 27
- 6
0
votes
1 answer
Scala context bounds "imitation" for abstract type parameters
I need to parse a json into a couple possible case classes:
trait Request {...}
case class RequestOne(...) extends Request
case class RequestTwo(...) extends Request
I created a request wrapper:
trait RequestModel {
type T <: Request
def…

Martee
- 77
- 10
0
votes
1 answer
In Scala how can I cast a value without asInstanceOf?
I have code that returns a value as Any. I have a way to get the actual type of this value. This is part of a very reflection-oriented solution.
// Reflecting on a class I have info about its constructor fields:
val fieldMembers:…

Greg
- 10,696
- 22
- 68
- 98
0
votes
1 answer
Scala reflection to cast using return type of method at runtime
I am using reflection in scala and i want to generalize the code to cast object to return type of another method which is identified at runtime using reflection
I have tried to get return type of method using reflection but not able to use this in…

Hemendra Yadav
- 11
- 2
0
votes
2 answers
Scala reflection: How to construct object and run its methods given 'String-type' class name and method name?
Given the name of a class, method and parameters from a third party library, how to create an object and invoke its method using scala reflection?
For example, the class name is "org.apache.spark.mllib.clustering.LDA", the method is "setK" and the…

Sherry Pan
- 3
- 2
0
votes
2 answers
Get constructor parameter values using scala reflection
Working in a codebase with scala that wants you, for certain classes, to define a sort of "make a new version" - so for instance if you have a class x(a :int, b:String, c:double)... it would have a function like this:
class x( a: Integer, b :…

Darren Oakey
- 2,894
- 3
- 29
- 55
0
votes
1 answer
How to instantiate user defined class with ToolBox.eval at runtime
I wrote the following code.
import scala.reflect.runtime.currentMirror
import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox
class A
object Main extends App {
val tb = currentMirror.mkToolBox()
tb.eval(tb.parse("new…

Mizunashi
- 313
- 1
- 7
0
votes
1 answer
In Scala.js, how would you traverse object graphs navigationally?
Suppose that you want to traverse an object graph in a navigational way, similar to the way we traverse file systems.
For example, imagine you have an object graph that supports this expression:
var x = objName.foo.bar.baz.fieldName
We can encode…

Ben McKenneby
- 481
- 4
- 15
0
votes
1 answer
How can I create an Option type at runtime (reflection)?
Using reflection, I have determined the runtime type of a thing, t: Type. Now I want to create a new Type of Option[t]. How can I do that?
val t: Type = ...
val optT: Type = ??? // Option of whatever t is
Why I want this: I have a handler…

Greg
- 10,696
- 22
- 68
- 98
0
votes
0 answers
Scala : Can't unquote List[scala.collection.immutable.HashMap[String,Double]]
I am using Scala reflection and toolbox to evaluate dynamic function as a string.
And I am trying to evaluate it with data of type List[HashMap[String, Double]]
But it is giving error
Can't unquote…

Tarun Khaneja
- 451
- 11
- 23
0
votes
0 answers
Can Scala reflection be used in a distributed way?
Background:
I have an app written in Scala and with the Akka toolkit. Furthermore, it has some Java dependencies, so it uses the Scala Reflection Library to call Java methods/classes when needed.
Problem:
There are cases in which multiple actors…

Need Answers Fast
- 67
- 2
- 7
0
votes
1 answer
case match a `reflect.runtime.universe.Type`
In Java we can do switch(value) {case(x): // do something;}
In Scala, we can do something similar with case match expressions:
val a = 1
a match {
case 1 => 1
case 2 => 2
} // 1
However, it doesn't work with a value of type…

yiksanchan
- 1,890
- 1
- 13
- 37