scala-macros

“dynamically” creating case classes with macros

こ雲淡風輕ζ 提交于 2019-11-27 12:12:32
I would like to create a macro generated hierarchy of sealed abstract and case classes. There was an example similar to this with http://docs.scala-lang.org/overviews/macros/typemacros.html but is is now obsolete. Is this still possible? I think it would be incredibly powerful to generate a type safe AST for some specified grammar. Ideally with an IDE able to resolve all the classes. Travis Brown First for some shameless self-promotion: Eugene Burmako and I are giving a talk on type providers , a closely related topic, at Scalar 2014 tomorrow, and I encourage you to take a look at the example

Dynamically create case class

别等时光非礼了梦想. 提交于 2019-11-27 07:17:42
问题 I am using a csv library that takes a case class and turns it into rows for me to read. The syntax is pretty close to File(path).asCsvReader[caseClass] . Link to library here However the problem is that I want to generate my case class from the tables in my database. I can receive the tables in my database and the types that the columns are (Int, Long, Double, String etc) but I do not know how to dynamically create a case class with that data since I do not know the information at compile

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

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 macros: What is the difference between typed (aka typechecked) an untyped Trees

那年仲夏 提交于 2019-11-27 04:21:15
问题 I'm getting started with scala macros, they're awesome, but I'm running into the difference between typed (aka typechecked) and untyped Tree s. For example, you can't call c.eval with a typechecked Tree for some reason. I can't find documentation on this 'typechecked' in the scala macros documentation (I know they're still working on that, this might be something that needs to be added some day). What does it mean for a Tree to be typechecked? Why are they so different that apparently c.eval

Scala Macros: Making a Map out of fields of a class in Scala

泄露秘密 提交于 2019-11-27 03:01:30
Let's say that I have a lot of similar data classes. Here's an example class User which is defined as follows: case class User (name: String, age: Int, posts: List[String]) { val numPosts: Int = posts.length ... def foo = "bar" ... } I am interested in automatically creating a method ( at compile time ) that returns a Map in a way that each field name is mapped to its value when it is called in runtime. For the example above, let's say that my method is called toMap : val myUser = User("Foo", 25, List("Lorem", "Ipsum")) myUser.toMap should return Map("name" -> "Foo", "age" -> 25, "posts" ->

Static return type of Scala macros

断了今生、忘了曾经 提交于 2019-11-26 17:37:31
问题 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 =

Howto model named parameters in method invocations with Scala macros?

自闭症网瘾萝莉.ら 提交于 2019-11-26 16:15:05
问题 There are use cases where it is useful to create a copy of an object which is an instance of a case class of a set of case classes, which have a specific value in common. For example let's consider the following case classes: case class Foo(id: Option[Int]) case class Bar(arg0: String, id: Option[Int]) case class Baz(arg0: Int, id: Option[Int], arg2: String) Then copy can be called on each of these case class instances: val newId = Some(1) Foo(None).copy(id = newId) Bar("bar", None).copy(id =

Getting subclasses of a sealed trait

时光总嘲笑我的痴心妄想 提交于 2019-11-26 16:05:33
问题 Is it possible (via macros, some form of Shapeless automagic or otherwise) to obtain a list of the subclasses of a sealed trait: At compile time? At runtime? 回答1: You don't need any 3rd party library to do this: sealed trait MyTrait case object SubClass1 extends MyTrait case object SubClass2 extends MyTrait import scala.reflect.runtime.{universe => ru} val tpe = ru.typeOf[MyTrait] val clazz = tpe.typeSymbol.asClass // if you want to ensure the type is a sealed trait, // then you can use clazz

Weird behavior trying to convert case classes to heterogeneous lists recursively with Shapeless

别来无恙 提交于 2019-11-26 09:40:27
问题 I stayed up way too late last night trying to figure out this Shapeless issue and I\'m afraid it\'s going to eat my evening if I don\'t get it off my chest, so here goes. In this minimized version I\'m just defining a type class that will recursively convert case classes into heterogeneous lists: import shapeless._ trait DeepHLister[R <: HList] extends DepFn1[R] { type Out <: HList } trait LowPriorityDeepHLister { type Aux[R <: HList, Out0 <: HList] = DeepHLister[R] { type Out = Out0 }