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
0 answers
Get FunctionX arguments and output types
I am struggling to obtained the types of the arguments of a defined function in Scala. For example Funcion1[T1, T2].
Since Java will eliminate the types checking (compiler warning: is unchecked since it is eliminated by erasure), I would like to…

aitorhh
- 2,331
- 1
- 23
- 35
0
votes
1 answer
Convert a Map to a nested case class
I want to convert a Map[String, Any] to a given case class, and the map can be a nested map. For example, personDataMap should be converted into Person("evan",24,Address(15213,"5000 Forbes Ave"))
case class Address(zip: Int, name: String)
case class…

yiksanchan
- 1,890
- 1
- 13
- 37
0
votes
1 answer
Scala : Error when using reflection to load class and methods
I have a class
class LrGen extends IModelGen {
var header = Array(
"First name",
"last name",
"address",
"city",
"state",
"zip"
)
override def getHeader(separator:String): String =
{
header.mkString(separator)
…

VictorGram
- 2,521
- 7
- 48
- 82
0
votes
0 answers
Avro4s generic toByteArray / fromByteArray
I has a problems with generic version toByteArray / fromByteArray functions for Avro4s
I found this Avro serialization with generic type issue
this is works:
def fromByteArray[A: SchemaFor : FromRecord](bytes: Array[Byte]): Option[A] = {
val…

HoTicE
- 573
- 2
- 13
0
votes
0 answers
Macros Scala reflect typeSymbol match results in GC overhead limit exceeded
I have a macros plugin that checks if an entity is a case class and then returns all fields in a dot notation.
Eg.
case class Language(name: String)
@Lenses
case class Page(a: Language)
So the main method would return
Seq("language.name")
This has…

raul782
- 479
- 1
- 5
- 15
0
votes
1 answer
Finding an overloaded method using Scala Reflections
I am trying to find the overloaded method using Scala reflections. Here's my code
import scala.reflect.runtime.universe._
object Example {
class Something {
def printIt(s1: String,s2: String) {println(s1 + s2) }
def printIt(s: Int) {…

bumblebee
- 1,811
- 12
- 19
0
votes
0 answers
Copy the contents of a Parent class object to child class object in scala
I have 2 classes.
Class A {
var something : String =_
}
Class B extends A{
var somethingElse : String =_
}
I will instantiate these 2 classes separately.
var a = new A
a.something ="test me"
var b = new B
before start…

D P
- 153
- 2
- 12
0
votes
2 answers
How do I bind types into my Scala Interpreter?
I am trying to bind values into an interpreter with types from an external library.
This is how I am currently declaring my interpreter:
val interpreter = new IMain({
val settings = new Settings
settings.usejavacp.value = true
…

Robby Zambito
- 3
- 2
0
votes
1 answer
In scala, How could Map[_,_] and scala.collection.immutable.Map[_,_] have different TypeTag?
They refer to the same thing, yet when I compare 2 type tags:
val ttg1 = typeTag[Map[_,_]]
val ttg2 = typeTag[immutable.Map[_,_]]
assert(ttg1.tpe == ttg2.tpe)
I got:
Map[_, _] did not equal…

tribbloid
- 4,026
- 14
- 64
- 103
0
votes
1 answer
How to get the aliased type of a type alias in scala runtime?
import scala.reflect.runtime.universe._
object Main {
final type INeedDetails = (Int, String, Unit, Nothing, Float)
def main(args: Array[String]): Unit = println {
typeTag[INeedDetails]
}
}
The snippet above will prnint…

Prikso NAI
- 2,592
- 4
- 16
- 29
0
votes
1 answer
Refer generic class by name
Consider the case that I want to deserialize a JSON string:
def deserialize[T](json)
I can provided class that I want to apply the function explicitly while writing code like
class Person(name: String)
deserialize[Person]("""{ "name": "Jennie"…

Thang Nguyen
- 1,110
- 8
- 17
0
votes
2 answers
Instance case class from companion object
I have the following:
trait C {}
object O {
case class Foo(bar: String) extends C
}
And I would like to instance Foo from a String. Until the moment I have achieved instanciate Foo, but I cannot cast to C. I am using:
val ob =…

ie8888
- 171
- 1
- 10
0
votes
1 answer
Find method in scala reflect by it's bytecode name
Is there a way to find a method by it's bytecode name?
For example, I would like to find a reference to println(Object) by string "_root_.scala.Predef.println(Ljava/lang/Object;)V."

Daniyar Itegulov
- 1
- 2
0
votes
0 answers
Reflectively determining compatibility of Scala generics
Given a target type (say List[String]) and some object o, the goal is to find a method of o with a return type that is compatible with the target type.
In the absence of generics, one can check this by comparing the target type and the return type…

NietzscheanAI
- 966
- 6
- 16
0
votes
1 answer
Pass class type to a function in Scala
I have a function that has this signature;
def process[E: TypeTag : ClassTag](id: Int): E = {
Normally I would call it like this:
process[Item](1)
I need to call it from the place where the type of the item is stored in a…

Vladimir Mikhaylovskiy
- 1,955
- 4
- 19
- 28