scala-2.10

Scala 2.10 reflection, how do I extract the field values from a case class, i.e. field list from case class

限于喜欢 提交于 2019-11-27 11:47:57
How can I extract the field values from a case class in scala using the new reflection model in scala 2.10? For example, using the below doesn't pull out the field methods def getMethods[T:TypeTag](t:T) = typeOf[T].members.collect { case m:MethodSymbol => m } I plan to pump them into for {field <- fields} { currentMirror.reflect(caseClass).reflectField(field).get } Travis Brown MethodSymbol has an isCaseAccessor method that allows you to do precisely this: def getMethods[T: TypeTag] = typeOf[T].members.collect { case m: MethodSymbol if m.isCaseAccessor => m }.toList Now you can write the

Static return type of Scala macros

别说谁变了你拦得住时间么 提交于 2019-11-27 07:04:27
So I've got this macro: import language.experimental.macros import scala.reflect.macros.Context class Foo class Bar extends Foo { def launchMissiles = "launching" } object FooExample { def foo: Foo = macro foo_impl def foo_impl(c: Context): c.Expr[Foo] = c.Expr[Foo](c.universe.reify(new Bar).tree) } I've said three times that I want foo to return a Foo , and yet I can do the following (in 2.10.0-RC3): scala> FooExample.foo res0: Bar = Bar@4118f8dd scala> res0.launchMissiles res1: String = launching The same thing happens if I remove the type parameters on either c.Expr . If I really want to

How to get ClassTag form TypeTag, or both at same time?

怎甘沉沦 提交于 2019-11-27 05:23:54
I have some code like this: class ReflectiveJsonFormat[T:TypeTag] extends JsonFormat[T] { def write(x: T) : JsValue = { val t = typeOf[T] val getters = t.declarations.filter { s => s.isMethod && s.asMethod.isGetter } val mirror = runtimeMirror(this.getClass.getClassLoader) val instanceMiror = mirror.reflect(x) } } That last line fails with: No ClassTag available for T I thought TypeTag was more info than a ClassTag ? Can I get the ClassTag from the TypeTag ? If not, is there some syntax for saying that T has two context bounds -- both TypeTag and ClassTag ? Or, how would you otherwise fix this

Getting a structural type with an anonymous class's methods from a macro

陌路散爱 提交于 2019-11-27 05:08:59
问题 Suppose we want to write a macro that defines an anonymous class with some type members or methods, and then creates an instance of that class that's statically typed as a structural type with those methods, etc. This is possible with the macro system in 2.10.0, and the type member part is extremely easy: object MacroExample extends ReflectionUtils { import scala.language.experimental.macros import scala.reflect.macros.Context def foo(name: String): Any = macro foo_impl def foo_impl(c:

Scala generic method - No ClassTag available for T

我们两清 提交于 2019-11-27 03:42:27
I'm relatively new to Scala and am trying to define a generic object method. However, when I refer to the parameterized type within the method I am getting "No ClassTag available for T". Here is a contrived example that illustrates the problem. scala> def foo[T](count: Int, value: T): Array[T] = Array.fill[T](count)(value) <console>:7: error: No ClassTag available for T def foo[T](count: Int, value: T): Array[T] = Array.fill[T](count)(value) ^ Thanks in advance for help in understanding what is wrong here and how to make this contrived example work. Régis Jean-Gilles To instantiate an array in

Use case of scala.concurrent.blocking

血红的双手。 提交于 2019-11-27 03:06:15
I came across the scala.concurrent.blocking method, and according to the Scala documentation this is... Used to designate a piece of code which potentially blocks, allowing the current BlockContext to adjust the runtime's behavior. Properly marking blocking code may improve performance or avoid deadlocks. I have some doubts: what is the factor with which new threads will be spawned? Is this applicable only for scala.concurrent.ExecutionContext.Implicits.global execution context or for user-created execution contexts as well? What happens if I wrap any executable with blocking { ... } ? Any

What's the easiest way to use reify (get an AST of) an expression in Scala?

对着背影说爱祢 提交于 2019-11-27 02:46:23
I'm looking at alternatives to -print or javap as a way of figuring out what the compiler is doing in Scala. With the new reflection/macros library, reify seems a good candidate for that, as shown in retronym's macrocosm 's desugar . It even shows how one used to do that, pre-M4. So the question is, what's the shortest/easiest thing I can type on Scala's REPL to get the AST for an expression, post-Scala 2.10.0-M4? kiritsuku A lot of things previously defined in package scala.reflect.mirror have moved to scala.reflect.runtime.universe : scala> import scala.reflect.runtime.{universe => u} import

Any way to obtain a Java class from a Scala (2.10) type tag or symbol?

二次信任 提交于 2019-11-27 02:36:42
问题 Looks like this gets me close, but (a) not quite (see below), and (b) using the string representation of a name feels like a hack... scala> import scala.reflect.runtime.universe._import scala.reflect.runtime.universe._ scala> val t = typeOf[Int] t: reflect.runtime.universe.Type = Int scala> t.typeSymbol.asClass.fullName res0: String = scala.Int scala> object X { class Y } defined module X scala> val y = typeOf[X.Y] y: reflect.runtime.universe.Type = X.Y scala> Class.forName(y.typeSymbol

How to know if an object is an instance of a TypeTag's type?

六月ゝ 毕业季﹏ 提交于 2019-11-27 02:36:34
问题 I have a function which is able to know if an object is an instance of a Manifest 's type. I would like to migrate it to a TypeTag version. The old function is the following one: def myIsInstanceOf[T: Manifest](that: Any) = implicitly[Manifest[T]].erasure.isInstance(that) I have been experimenting with the TypeTags and now I have this TypeTag version: // Involved definitions def myInstanceToTpe[T: TypeTag](x: T) = typeOf[T] def myIsInstanceOf[T: TypeTag, U: TypeTag](tag: TypeTag[T], that: U)

What is scala's experimental virtual pattern matcher?

淺唱寂寞╮ 提交于 2019-11-27 01:44:41
问题 I've seen quite a few mentions recently of the new "virtualized" pattern matcher for scala. I missed the memo explaining what it actually was... 回答1: The "virtualized" pattern matcher is a rewrite of the existing matcher. The motivation for doing this was to support virtualization of pattern matching for the polymorphic embedded DSLs , not relevant for 2.10. As Iulian says in the comments below: It's very similar to how for-comprehensions are compiled: instead of directly generating code,