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
0 answers

How to recognize an outer variable which is listed between members by reflection?

I have a function which constructs a map by reflection from an object literal (the purpose is to make porting some JavaScript code easier). The function works fine, but I have found a case in which the reflections lists variables from an enclosing…
Suma
  • 33,181
  • 16
  • 123
  • 191
2
votes
1 answer

Access to annotation value in Scala 3.0

I created annotation in scala and used it as follows: object Main extends App { println(classOf[Annotated].getAnnotations.length) import scala.reflect.runtime.universe._ val mirror = runtimeMirror(cls.getClassLoader) } final class…
Nik Kashi
  • 4,447
  • 3
  • 40
  • 63
2
votes
1 answer

In scala 2.13, why sometimes TypeTags cannot be inferred? And how to construct one from a variable symbol?

Here is a simple example in shapeless: it("from Witness") { val ttg = implicitly[TypeTag[Witness.Lt[String]]] val ctg = implicitly[ClassTag[Witness.Lt[String]]] } it("... from macro") { val ttg =…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
2
votes
2 answers

In Scala, is it possible to instantiate an object of generic type T?

In Scala, even if the solution is not elegant, is it possible to instantiate/create a new object of a generic type T? Is it possible to achieve this using reflection? For example, I am interested in something like the following: case class…
code
  • 5,294
  • 16
  • 62
  • 113
2
votes
1 answer

How to use quasiquotes with previously defined object

I just started studying scala compile-time reflection, and I got introduced to quasiquotes by the Scala official guides. One concept I'm still struggling with is how am I supposed to work with quasiquotes (or reify, for that matter) if I want to…
2
votes
2 answers

How to compare the return type of a method to a Scala native or TypeTag?

So, I'm using the Scala reflections library, and I'm trying to check if a method conforms to a given type. To simplify, I'm trying to check only its output. What I have now is: val returnType = methodSymbol.returnType // returnType:…
Lucas Lima
  • 832
  • 11
  • 23
2
votes
1 answer

Instantiating Scala class/case class via reflection

Description I'm trying to build a tool capable of converting a Map[String, Any] into a class/case class instance. If the class definition contains default parameters which are not specified in the Map, then the default values would apply. The…
2
votes
2 answers

How to get generic simple class name from scala TypeTag?

How can I get the simple class name including generic using TypeTag? I think that the method signature should be like: def getClassName[A: TypeTag](a: A): String getClassName(Map("a" -> 123)) should return Map[String,Int]. Things I've tried: def…
krismath
  • 1,879
  • 2
  • 23
  • 41
2
votes
1 answer

In Scala, why it is impossible to infer TypeTag from type alias or dependent type?

I have a simple scala program to test the Capability of Scala to infer type classes: import scala.reflect.ClassTag object InferTypeTag { import org.apache.spark.sql.catalyst.ScalaReflection.universe._ def infer(): Unit = { type U = (Int,…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
2
votes
1 answer

In scala 2 or 3, is it possible to debug implicit resolution process in runtime?

In scala language, implicit resolution is often done in compile-time and sometimes throws obfuscating error information, one famous example of such error is when shapeless Generic throws error information like: error: could not find implicit value…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
2
votes
0 answers

A toArray function that breaks on primitive type, but works fine when written as a PartialFunction?

Playing around with a reflection-base code I've come a cross a situation where I want to convert a sequence into array. The catch is that the only type information available is in the form of a runtime.universe.Type, and the sequence itself is of…
Worakarn Isaratham
  • 1,034
  • 1
  • 9
  • 16
2
votes
1 answer

How to compile and run Scala code programmatically

I have the following code and I would like to compile it on the fly and run it. object HelloWorld { def main(args: Array[String]): Unit = { println("Hello, world!") } } So far I have tried something like below: import…
Vishal
  • 19,879
  • 23
  • 80
  • 93
2
votes
1 answer

Call scala method from object dynamically

I have a scala case class and object like below, case class User(userId: Long, UserName: String, ts: Timestamp) object User { def getRdd(rdd: RDD[JsValue], type : String): RDD[User] = { val rdd1: RDD[User] = rdd.map(doc =>…
Nachiket Kate
  • 8,473
  • 2
  • 27
  • 45
2
votes
0 answers

Inconsistent result when checking type subtype

Sometimes I get inconsistent result when I check if a class extends a trait. I'm using scala 2.12.8. My project uses ru.Type to dynamically build a chain of operations. The output of an operation must conform to the input of the next one. Before…
To-om
  • 75
  • 6
2
votes
3 answers

No TypeTag available for case class Type

I want to generate a method which will convert an Object into a Map[String, _], and later back from Map[String, _] to Object. I generate the initial object as follows: case class Name (firstName : String, lastName : String) case class Documents…
Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94