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
1
vote
1 answer

Scala accessing members of runtime types using reflection of a class that extends a trait

Suppose I have a MyItem trait and its companion object has an apply() function that creates a class instance called SubItem that extends from MyItem: import scala.reflect.runtime.{universe => ru} trait MyItem { import MyItem._ def num:…
Hush
  • 13
  • 2
1
vote
1 answer

How to access scala annotation in scala code

I have defined a simple scala annoation and use it to annotate my case class Person: package com.my import scala.annotation.StaticAnnotation @scala.annotation.meta.field class Description(value: String) extends StaticAnnotation{ } Then I use it…
Tom
  • 5,848
  • 12
  • 44
  • 104
1
vote
2 answers

How to call Databricks dbutils using Scala Reflection / Mirrors

I want to call com.databricks.dbutils_v1.DBUtilsHolder.dbutils.secrets.get(scope = "myScope", key = "myKey") using mirroring. So far I achieve this: val className = "com.databricks.dbutils_v1.DBUtilsHolder" import scala.reflect.runtime.{universe…
1
vote
1 answer

Write method to a class dynamically at runtime in scala and create a jar

I would like to understand is there a way to write a method to existing class at runtime and to create a jar dynamically in scala. So far i tried to create a class dynamically and able to run it thru reflection, however the class is dynamic class…
user3569397
  • 27
  • 1
  • 8
1
vote
1 answer

Scala invoke Object apply method using string

I have a class MyFilter which has many different versions. Depending on the input version, I want to invoke that object and apply that filter. Sample is shown below: object v1{ def apply(l: List[String]) = { l.filter(_.contains("v1")) …
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
1
vote
2 answers

Scala reflection to get List field value by universe apply

I defined an annotation class with a List field in Scala: class TestAnnotation(a: String, b: List[String]) extends StaticAnnotation And some classes using the annotation, e.g.: @TestAnnotation("test", List("b1", "b2")) class TestA Now I want to…
W.Bing
  • 11
  • 1
1
vote
2 answers

How to get the declaring class / trait using Scala reflection?

Using Java reflection, I can make: MyObject .getClass .getMethods .map(_.getDeclaringClass()) For each method declared in the Object MyObject, I will get the declaring class of that method / value - what I'm interest here is, specifically, in…
Lucas Lima
  • 832
  • 11
  • 23
1
vote
1 answer

Scala: verify class parameter is not instanceOf a trait at compile time

At compile time I want to verify that a class parameter is NOT an instance of a particular trait T. I know how to do it at runtime using require or a case match but wondering how this might be done at compile to prevent users from providing certain…
bawejakunal
  • 1,678
  • 2
  • 25
  • 54
1
vote
1 answer

Scala equivalent of Java `Class` class?

In Java, there is a Class class that represents, well, classes. It's useful when you need to do some class checks dynamically at runtime, for example, if some interface can accept varied number of arguments and it expects the arguments to have…
siledh
  • 3,268
  • 2
  • 16
  • 29
1
vote
2 answers

How to get underlying constant type from singleton type with Scala reflection API

import scala.reflect.runtime.universe._ val a: 42 = 42 val t: Type = typeOf[a.type] assert(getConstantType(t).get =:= typeOf[42]) def getConstantType(t: Type): Option[ConstantType] = ??? How could I generally implement getConstantType so that the…
user3773003
  • 101
  • 6
1
vote
1 answer

Changing Return type of a function in Scala

I need to write a function that has the return type NodeSeq (scala.xml.NodeSeq), In the below function, I'm trying to get the NodeSeq of an XML file, by evaluating an expression using the below code. The compiler throws type mismatch error. import…
chqdrian
  • 335
  • 4
  • 17
1
vote
1 answer

WeakTypeOf from type parameter

I have a simple code with function which gets weakTypeOf case class and returns its fields, predictably we get 2 items list def getMembers[T: WeakTypeTag] = weakTypeOf[T].members.filterNot(_.isMethod).toList final case class Person(name: String,…
Dmitry Reutov
  • 2,995
  • 1
  • 5
  • 20
1
vote
1 answer

In scala 2.12, why none of the TypeTag created in runtime is serializable?

I'm seeking a method to create a serializable TypeTag without using compile time tools (completely relying on runtime). This is a basic feature for all reflective language. The answer in this post proposed a few methods: In Scala, how to create a…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
1
vote
1 answer

In scala, how to create a ClassTag of a specialised class using ClassTag of its generic member?

Let's use the common Array[T] class as an example: import scala.reflect.ClassTag { val t = implicitly[ClassTag[String]] val ts = implicitly[ClassTag[Array[String]]] Seq(t, ts).foreach(println) } this will give the following…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
1
vote
1 answer

Instantiating an empty object of parametric type

I'm trying to make a CSV reader that parse a CSV file with a header into a list of "line object". Those "line objects" are of a type given by the caller of the function. The idea is to be able to use the CSV reader like that: case class…
nefas
  • 1,120
  • 7
  • 16