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
3
votes
1 answer
How to type cast Any dynamically in scala?
I want to convert "Any" object into an object of runtime type. Based on the class name (string) at the runtime, how do I convert an ANY object to actual object?
I tried using converting class name into class object using Class.forName
val clazz =…

sen
- 198
- 2
- 9
3
votes
1 answer
Extract Type T from Seq[T]
I have a case class which has got two fields which are a Seq[Objects]. These two objects belong to two different case classes.
case class Passport(country: String, name: String)
case class DrivingLicence(code: String, name: String)
case class…

Ignacio Alorre
- 7,307
- 8
- 57
- 94
3
votes
1 answer
Protect system when using scala interpreter api
I have created simple REPL bot for scala.
It runs in linux environment and processes written scala code in dialogs and gives the result:
For example
user| 1+1
bot | res0: Int = 2
user| res0 + 3
bot | res1: Int = 5
...
I used scala interpreter API…

oybek
- 630
- 4
- 14
3
votes
0 answers
Scala: How to get a non-erased class instance with implicits without using asInstanceOf?
I have the following Java method:
public void foo(Class parentClass, Class extends T> childClass);
I tried to use an implicit ClassTag to call the above method, but unfortunately the runtime methods returns an erased class, i.e., Class[_],…

Gal
- 5,338
- 5
- 33
- 55
3
votes
1 answer
Scala Reflection exception during creation of DataSet in Spark
I want to run Spark Job on Spark Jobserver.
During execution, I got an exception:
stack:
java.lang.RuntimeException: scala.ScalaReflectionException: class
com.some.example.instrument.data.SQLMapping in JavaMirror with
…

addmeaning
- 1,358
- 1
- 13
- 36
3
votes
1 answer
Manually creating a type tag
I have some Scala code with type tags that I need to make available for Java users. My problem boils down to solving the following:
Given two Scala type tags, how can I manually create a type tag for Tuple2[ A , B ]?
If it makes any difference my…

harel
- 525
- 5
- 21
3
votes
1 answer
Scalameta: Identify particular annotations
I want to auto-generate REST API models in Scala using scalameta annotation macros. Specifically, given:
@Resource case class User(
@get id : Int,
@get @post @patch name : String,
@get @post email :…

pathikrit
- 32,469
- 37
- 142
- 221
3
votes
1 answer
Why does this public field have a PRIVATE flag?
I'm writing a Scala macro and am traversing the tree to find non-private fields in classes.
Consider this code that the macro looks at:
class Foo {
val bar: String = "test"
}
I'm traversing this code and getting to bar's ValDef. It has only two…

Omer van Kloeten
- 11,800
- 9
- 42
- 53
3
votes
2 answers
Preserving type arguments in Akka receive
This question has kind of been answered by Roland Kuhn in this post, however, despite several comments asking for detail, he didn't bother to share the complete answer.
Here's what I want to do: I have a wrapper class case class Event[T](t: T) of…

typeduke
- 6,494
- 6
- 25
- 34
3
votes
1 answer
How to get return type string of a method using Scala reflection?
consider the following code:
object Foo{def foo(a:Int):List[(String, Int)] = ???}
class Bar{def bar(a:Int, b:Any):Option[(String, Long)] = ???}
Given either the object or class, I need to first find the method names (does not seem to be that…

Jus12
- 17,824
- 28
- 99
- 157
3
votes
1 answer
How to make implicits available to reflection
Using the Scala (runtime) relection API, I'm trying to compile code that makes heavy use of implicits (actually the spire.math library):
val src = "(a: spire.math.Jet[Double],b: spire.math.Jet[Double]) => a + b"
println(…

NietzscheanAI
- 966
- 6
- 16
3
votes
2 answers
Scala: Dynamically generating match clauses for case classes
I want to use the power of Scala's pattern matching within a set of `condition-action' rules. These rules are not known in advance, but rather are generated at runtime according to some complex critera. The algorithmic generation mechanism can be…

NietzscheanAI
- 966
- 6
- 16
3
votes
2 answers
Get precise return type from a TypeTag and a method
Say I have
trait Foo[T] { def bar: Bar[T] }
and want to obtain return type of bar when called on a Foo[Int] (i.e. Bar[Int] in this case) from the type tag of Foo[Int] and the name "bar" (we can assume there are no overloads or we can tell them…

Alexey Romanov
- 167,066
- 35
- 309
- 487
2
votes
1 answer
How can I run generated code during script runtime?
During the running of a scala script, I would like it to generate some code and execute this.
I thought I had found two examples online that might work, but they aren't successful
import scala.reflect.runtime.universe._
import…
user13800089
2
votes
1 answer
scala pass T type to Java constructor
I am using Jackson read Yaml in scala 2.11/2.12 val mapper = new ObjectMapper(new YAMLFactory()), I think the Java constructor I am calling is
public T readValue(String content, Class valueType)
this code works
def load(): SomeClass {
…

Litchy
- 623
- 7
- 23