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
5
votes
1 answer
Scala parser cuts last bracket
Welcome to Scala 2.12.1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_121).
Type in expressions for evaluation. Or try :help.
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.reflect.runtime._
import…

Andrzej Jozwik
- 14,331
- 3
- 59
- 68
4
votes
1 answer
What Scala 3 syntax can match on a Type and its Type parameters in the context of a macro?
From the context of a Scala 3 Macro:
def aMacroImpl[T](using Type[T], Quotes): Expr[SpecialMap[_, _, _]] = {
import quotes.reflect._
val TRepr: TypeRepr = TypeRepr.of[T]
val TSym: Symbol = TRepr.typeSymbol
val SrtdMapSym: Symbol =…

Ben McKenneby
- 481
- 4
- 15
4
votes
2 answers
How to obtain a tree for a higher-kinded type parameter in a scala macro
I'm trying to write a macro to simplify some monad-related code (I'm using cats 1.6.0 for the Monads). For now I just want to be able to write lift[F](a) where F is a unary type constructor, and have that expand to a.pure[F]. Seems simple enough,…

Matthias Berndt
- 4,387
- 1
- 11
- 25
4
votes
0 answers
Scala UDFs as external Jars
I have a object where I have written my Udf
package com.udf.sample
object UDFDetails {
def udfIndexOf = (inputValue: String, matchCriteria: String) => {
inputValue.toUpperCase().indexOf(matchCriteria)
}
}
If I register it directly then I get…

merdem9456
- 43
- 7
4
votes
0 answers
Pattern matching with ClassTag or Manifest: Code Smell?
I'm wondering if using a generic type, known at runtime through ClassTag or Manifest is not in fact a code smell, leading to unpredictable results.
Here is a example of what can happen:
import scala.reflect._
trait BaseTrait
trait OtherTrait…

Kam
- 41
- 2
4
votes
2 answers
Define spark udf by reflection on a String
I am trying to define a udf in spark(2.0) from a string containing scala function definition.Here is the snippet:
val universe: scala.reflect.runtime.universe.type = scala.reflect.runtime.universe
import universe._
import…

sourabh
- 466
- 4
- 13
4
votes
1 answer
Preserve method parameter names in scala macro
I have an interface:
trait MyInterface {
def doSomething(usefulName : Int) : Unit
}
I have a macro that iterates over the methods of the interface and does stuff with the method names and parameters. I access the method names by doing something…

mushroom
- 6,201
- 5
- 36
- 63
4
votes
1 answer
Strange type mismatch with a macro: found: singleton type with underlying type A, required: A
I have
class Foo[A] {
def foo[B](x: A, y: B) = y
}
class Bar[A] extends Foo[A] {
override def foo[B](x: A, y: B) = superCall
}
where superCall whitebox macro should expand to super.foo[B](x, y), and that's what -Ymacro-debug-lite shows. The…

Alexey Romanov
- 167,066
- 35
- 309
- 487
4
votes
1 answer
Scala reflection: knownDirectSubclasses only works for sealed traits?
Given this question and SI-7046, this isn't at all what I expected.
scalac test.scala && scala Test in Scala 2.11.6 on the following prints an empty Set():
trait Foo
case class Bar() extends Foo
case class Baz() extends Foo
object Test {
def…

helgridly
- 133
- 8
4
votes
2 answers
Why is reflect.runtime.universe.RuntimeClass inferred over Nothing when reflect.runtime.universe._ is present?
I've stumbled upon a strange situation where having reflect.runtime.universe._ imported causes reflect.runtime.universe.RuntimeClass to be inferred where it seems Nothing would be more appropriate.
Consider this simple method and List:
import…

Michael Zajac
- 55,144
- 7
- 113
- 138
4
votes
1 answer
Why does the method runtimeClass of ClassTag return a wildcard class?
The runtimeClass method of a ClassTag[T] returns a Class[_]. I would expect it to return a Class[T], since we know the ClassTag[T] represents the (runtime) class of T.
Why does it return "any" Class? Is there a fundamental reason for that?

gzm0
- 14,752
- 1
- 36
- 64
3
votes
1 answer
Scala runtime reflections get all the members of a specific type even for inner classes
With scala 2.12.10
Suppose I want to implicitly convert at runtime a case class, in this case Special to a case class SpecialString. The implicit conversion is provided by a trait External. The name for SpecialString should be the declaration name…

Enrico Tolotto
- 33
- 3
3
votes
2 answers
How to get field names and field types from a Generic Type in Scala?
In Scala, given a generic type T, how to retrieve the list of field names and the field types? For example, if I have the case class:
case class Person(name: String, age: Int, gender: Boolean)
And the generic function:
def…

code
- 5,294
- 16
- 62
- 113
3
votes
0 answers
Register UDF dynamically in spark using scala reflection
Problem:
My program accepts a JSON string as inpu, which contains a udf name, function definition and type. eg.
{
"udfList": [
{
"name": "add"
"definition": "(x: Int, y:int) => x+y"
"type": "(Int, Int) => Int"
}
]
}
The…

Eapen Jose
- 71
- 4
3
votes
1 answer
How to use Scala annotations in Java code
Is there any way to use annotations defined in Scala so they can work with Java code? I have a library with some annotations I would like to use in Java

Maciej Laskowski
- 697
- 1
- 5
- 7