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
0
votes
1 answer

Invoke a template Scala function with a type stored as wild card classTag?

I have a following class defined to store the classTag of an object: case class BindObj (bindTypeA: ClassTag[_], bindTypeB: ClassTag[_]) { } and a template function as below: def workOnThis[A, B](left: A, right: B): Unit => { …
user3761728
  • 97
  • 1
  • 2
  • 8
0
votes
1 answer

Is there a better way to test the generic type (without type arguments) in Scala?

Given I'm using reflection to go through a list of members (giving me runtime.universe.Symbol), how can I check the generic type without the type arguments? In other words, how do I find the members which are List[] generic type, regardless of what…
Daniel
  • 8,655
  • 5
  • 60
  • 87
0
votes
0 answers

"No TypeTag available for" with generics

I have this piece of code currently import org.apache.spark.sql.catalyst.ScalaReflection import org.apache.spark.sql.types.StructType case class SpecificMessage(id: String) trait Deserializers { val schema = ScalaReflection …
Prassi
  • 73
  • 7
0
votes
0 answers

Ad-hoc polymorphism for reflection created case classes Scala

I need to implement Ad-hoc polymorphism, consider this code: trait BlockInputRequest case class StaticBlockInput( content: String ) extends BlockInputRequest case class StaticBlockOutput( content: String = "" ) trait…
0
votes
2 answers

scala get generic type by class

Is it possible pass generic parameter using class: def m(clazz: Class[_]): Unit = { m2[] //here I want to exact type (that I should have because I have Class) } def m2[C: ClassTag]() : List[C] = { println("hi") List.empty } Any…
SLA VA
  • 53
  • 3
0
votes
0 answers

In scala 2.13, how to use actual TypeTag in macro?

This capability seems to be dropped by SI-6186, and no mechanism could be used to replace it I found the following remarks in its commit message: Now we restore the broken balance by banning TypeTag from macro impls. This forces anyone to use…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
0
votes
2 answers

In scala macro, how to lift an object and use it in quasiquote at compile time?

The following code snippet is a short scala macro bundle definition from a thoughtworks project: private[SelfType] final class Macros(val c: whitebox.Context) { import c.universe._ def apply[A: WeakTypeTag]: Tree = { val a =…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
0
votes
1 answer

Scala reflection: get all constructor arguments of a specific type

I want to create a general purpose method def getOptionalArgs[T](): List[String] such that when passed a type, it returns the list of all arguments to that type that are an Option[_] E.g. I have a case class Foo(bar: String, baz: Option[String]). In…
Logister
  • 1,852
  • 23
  • 26
0
votes
0 answers

In scala, how to authentically get the declared type of a variable?

I recently found that the scala reflection library seems to be out of maintenance and interact poorly with a few other features, e.g. For the most simple use case, when the types of variables needs to be extracted, scala reflection can't get it…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
0
votes
1 answer

How to define a class at runtime that includes a call to a method of an object without a companion class?

The following Scala (2.12.12) code runs with the expected behavior. The ClassDef tree is defined using the toolbox, and a ClassSymbol is returned. class MyObj object MyObj { def foo(x: Int): Int = x } object Test { import ru._ def…
Erp12
  • 600
  • 3
  • 16
0
votes
1 answer

Is it possible to get schema from a type field in trait?

I have many HIVE tables, and I want wrap query by trait. trait Trait1 { type DT <: Product val SCHEMA = org.apache.spark.sql.catalyst.ScalaReflection.schemaFor[DT].dataType.asInstanceOf[org.apache.spark.sql.types.StructType] } object O1 extends…
0
votes
1 answer

How to reflect concrete types that corresponds to the type parameters of an abstraction type in Scala?

Suppose we have a generic type (for example, Seq[E]) and a concrete subtype (for example, Seq[Int]). How can we extract concrete type that corresponds to the type parameters of the abstraction type. In other words, how can we know E -> Int. Below is…
Erp12
  • 600
  • 3
  • 16
0
votes
2 answers

How to find class parameter datatype at runtime in scala

import scala.reflect.runtime.universe import scala.reflect.runtime.universe._ def getType[T: TypeTag](obj: T) = typeOf[T] case class Thing( val id: Int, var name: String ) val thing = Thing(1, "Apple") val dataType =…
0
votes
1 answer

Implicit resolution fail in reflection with ToolBox

I am trying to generate Avro4s's RecordFormat in reflection based on class path. The following code throws an error. case class MyCaseClass(a: Int) println(toolBox.compile { toolBox.parse( s""" |import com.sksamuel.avro4s._ …
lalala
  • 63
  • 1
  • 6
0
votes
1 answer

How to reflect concrete return types for methods of classes defined at runtime using the Scala ToolBox?

When reflecting the foo() method of the class Cls we can easily get the concrete return type using the following. class Cls { def foo() = List("A", "B") } val classType = ru.typeOf[Cls] val classMirror =…
Erp12
  • 600
  • 3
  • 16