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
2
votes
0 answers
How to instantiate a generic class using reflection in scala
I have a class hierarchy beginning with these traits:
sealed trait MessageBody
sealed trait MessageKey[T <: MessageBody]
I need to build automatically a list of all direct subclasses of MessageKey. With searching here and there I came up to this…

Mahdi
- 1,778
- 1
- 21
- 35
2
votes
1 answer
Scala Fork-Join-All With Multiple Generic Types and 1 Generic Unit of Work
I'm attempting to write a method which accepts multiple generic types and takes as an argument a unit of work to execute.
The idea is that the unit of work is a common function that itself is generic. For the sake of example, let's say it's…

medge
- 598
- 5
- 16
2
votes
2 answers
Get typeOf[this.type] in subclass
How can one let a superclass have access to its concrete instance's type?
class Base {
val t = typeOf[this.type]
}
class X extends Base {
}
assert((new X).t =:= typeOf[X]) <-- fails!!!
So, the idea is that Base.t should reflect the concrete…

user3612643
- 5,096
- 7
- 34
- 55
2
votes
2 answers
Getting method's function type from the MethodMirror instance in Scala
Assume I have an instance of MethodMirror created for a certain method of an object. By mirror's fields I can easily access return type and parameters of the method. But I actually need to obtain the type this method would have as a function.
Here…

Mr 525
- 75
- 8
2
votes
1 answer
Scala Reflection: Invoking a Function1's apply method - multiple alternatives?
I'm trying to invoke a function using the scala reflection api in v2.11.6:
import scala.reflect.runtime.{universe => ru}
def f(i: Int) = i + 2
val fn: Any = (f _)
val ref = ru.runtimeMirror(ru.getClass.getClassLoader).reflect(fn)
val apply =…

Pyetras
- 1,492
- 16
- 21
2
votes
1 answer
type parameter mismatch with WeakTypeTag reflection + quasiquoting (I think!)
Inspired by travisbrown, I'm trying to use a macro to create some "smart constructors".
Given
package mypkg
sealed trait Hello[A]
case class Ohayo[A,B](a: (A,B)) extends Hello[A]
and
val smartConstructors = FreeMacros.liftConstructors[Hello]
The…

arya
- 946
- 5
- 14
2
votes
1 answer
Scala macros referring to a member type
I have a trait with a member type, and want to have a macro with signature containing this type:
trait Foo {
class Bar[A] { ... }
def baz[A](x: Bar[A]): Bar[A] = macro bazImpl[A]
def bazImpl[A: c.WeakTypeTag](c: blackbox.Context)(x:…

Alexey Romanov
- 167,066
- 35
- 309
- 487
2
votes
1 answer
How to instantiate a Scala object using reflection
I have some code that uses reflection to instantiate a Java or Scala class, allowing a user to specify the name:
Assume that loadIt below is a hypothetical method defined using this approach.
def getInstance(name:String, jar:String) = {
val…

Jus12
- 17,824
- 28
- 99
- 157
2
votes
1 answer
Comparing Scala reflection Symbols
The Types Scaladoc page warns:
Type Equality can be checked with =:=. It's important to note that == should not be used to compare types for equality-- == can't check for type equality in the presence of type aliases, while =:= can.
There is no…

Alexey Romanov
- 167,066
- 35
- 309
- 487
2
votes
1 answer
Dealiasing Types in Scala reflection
How can I resolve aliases given a Type? I.e.
import reflect.runtime.universe._
type Alias[A] = Option[Option[A]]
val tpe = typeOf[Alias[_]]
val ExistentialType(quantified, underlying) = tpe
How do I get Option[Option[_$1]] from underlying (or…

Alexey Romanov
- 167,066
- 35
- 309
- 487
2
votes
1 answer
Is there a way to get the direct parents of a ClassSymbol in macro context?
Im trying to get the direct super classes / traits of a ClassSymbol. The method baseClasses() does not work for me as it also includes the super super.. types.
The java.lang.Class.getSuperclass() and java.lang.Class.getInterfaces() would actually be…

Taig
- 6,718
- 4
- 44
- 65
2
votes
2 answers
How to get to type parameters of a reflect.runtime.universe.Type in scala?
Suppose I get a Type representing List[Int]:
> import scala.reflect.runtime.universe
> val mirror = universe.runtimeMirror(this.getClass.getClassLoader)
mirror: reflect.runtime.universe.Mirror = JavaMirror with ...
> class X{ def…

user48956
- 14,850
- 19
- 93
- 154
1
vote
1 answer
Creating syntactic sugar for a scala argument to give the illusion of a different type
I am creating a library and am exposing a class called A that has in it defined a function func(f: SomeType => A). It is imperative that func is called with the following syntax:
val x = new A()
val y = new A()
x.func(_ => y)
However in reality I…

B-Brennan
- 111
- 9
1
vote
1 answer
Scala type pattern matching
I want to implement a pattern matching over types of case class.
Caurrently I have this idea in mind:
val T = typeOf[Int]
val S = typeOf[String]
// other primitive types
val O = typeOf[Option[_]] // doesn't work for all generic types
val X =…

Lyashko Kirill
- 513
- 3
- 14
1
vote
1 answer
Determine the function signature of an anonymous function in scala
The context is to register a UserDefinedFunction(UDF) in spark, where the UDF is an anonymous function obtained via reflection. Since the function signature of the function is determined at runtime, I was wondering whether it is possible to do…

Hang Wu
- 15
- 4