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
2 answers
Scala case classes and recursive reflection
Given 2 Scala case classes
case class Bar(x: Int)
case class Foo(b: Bar, z: Double)
I have a piece of code that prints the types of Foo fields using reflection:
import scala.reflect.runtime.universe._
def f[T: TypeTag] =…

outlaw
- 241
- 4
- 16
0
votes
1 answer
Scala eval function that gets a String with variables
I want to be able to evaluate the expression that returns from func.
The problem is that the expression includes the variable a, which is not familiar in the scope of func but is familiar in the scope of playground.
I want to be able to send the…

Nili Al
- 21
- 3
0
votes
1 answer
Invoke all methods on a Scala object using reflection
Given the following object
object functions {
type MyOutput = String
def f1: MyOutput = "res1"
def f2: MyOutput = "res2"
def f3(foo: String): Boolean = foo.equals("bar")
def f4(in:String): MyOutput = in+"res4"
}
Is it possible to…

Hektor J.G.
- 15
- 5
0
votes
2 answers
Scala 3 Macros: How to invoke a method obtained as a `Symbol` in a quoted code block?
In a Scala 3 macro that takes a type parameter T, you can use TypeRepr.of[T] and the new Scala 3 reflection API to explore the companionClass of T, and find the Symbol for an arbitrary method on that companion class (eg…

Roberto Tyley
- 24,513
- 11
- 72
- 101
0
votes
2 answers
Scala Reflection issue on accessing case class attributes
I have been able to get a List of attributes for a case class using scala with Reflection.
import scala.reflect.runtime.universe._
def classAccessors[T: TypeTag]: List[MethodSymbol] = typeOf[T].members.collect {
case m: MethodSymbol if…

thebluephantom
- 16,458
- 8
- 40
- 83
0
votes
1 answer
Getting Case Class definition which points to another Case Class
I am looking at getting case class definitions.
From SO I gleaned this practice as per Get field names list from case class, the answer using reflection by Dia Kharrat.
Some experimenting in which I have a case class referring to another case class,…

thebluephantom
- 16,458
- 8
- 40
- 83
0
votes
1 answer
scala cast object based on reflection symbol
I have a Scala reflection Symbol.
val symbol = scala.reflect.api.Symbol
how can I cast an object
val obj: Any
to the type of that Symbol ?
reason why I am asking is that I have a implicit method to convert Map[String, Any] to a Scala case class, but…

linehrr
- 1,668
- 19
- 24
0
votes
1 answer
Scala whitebox macro how to check if class fields are of type of a case class
I am trying to generate a case class from a given case class that strips of Option from the fields. It needs to this recursively, so if the field itself is a case class then it must remove Option from it's fields as well.
So far I managed to it for…

Aiono
- 389
- 2
- 10
0
votes
1 answer
Scala generic method - No ClassTag available for T - when using Collection
I want to leverage Scala reflection to implicitly pass a ClassTag.
There are plenty solutions on StackOverflow on how to accomplish this.
import scala.reflect.ClassTag
// animal input
trait AnimalInput {}
case class DogInput() extends…

Adam Berry
- 108
- 1
- 3
0
votes
1 answer
Trying to extract the TypeTag of a Sequence of classes that extend a trait with different generic type parameters
The following code example shows the core of my question:
// This is the base trait that the classes are extending
trait Operation[T] {
def operate(x: T): T
}
// Here are 2 case classes that exist for the sole purpose of being
// the generic type…

Koedlt
- 4,286
- 8
- 15
- 33
0
votes
0 answers
Avoiding garbage while creating objects using Scala runtime reflection
In the example code below, I am trying to create case class objects with default values using runtime Scala reflection (required for my use case)!
First Approach
Define default values for case class fields
Create objects at runtime
Second…

iamsmkr
- 800
- 2
- 10
- 29
0
votes
0 answers
Named parameters in Scala annotation
I have a Scala annotation with parameters, some with default values:
case class A(x: String, y: Option[String] = None, z: Option[Boolean] = None)
extends scala.annotation.StaticAnnotation
The goal is to annotate classes with this, and collect…

Alex Savitsky
- 2,306
- 5
- 24
- 30
0
votes
1 answer
How do I fetch the overridden member of a sealed trait via reflection?
I'm trying to write a generalised function that takes a Type of SomeSealedClass and uses reflection to return a list of values overridden by the trait's child classes/objects.
sealed abstract class Sealed(val name: String)
object Sealed {
case…

Turbo123
- 35
- 1
- 6
0
votes
1 answer
Scala how to fix type erasure warning even though I added ClassTag
I am wondering how to fix the type erasure warning even though I added ClassTag? is there an easy fix without adding an additional library like TypeTag?
import scala.reflect.ClassTag
class Node
class C_PHYLUM[T]
class I_PHYLUM[T](name:…

Node.JS
- 1,042
- 6
- 44
- 114
0
votes
1 answer
Scala - How to extract Json4s with dynamic case class created with ToolBox
I defined the case class dynamically using Toolbox.
And when I do extract of json4s, I get the following exception:
import org.json4s._
import scala.reflect.runtime._
import scala.tools.reflect.ToolBox
implicit val formats =…