Questions tagged [scala-macros]

Scala macros is a facility for Scala—a general-purpose programming language—that permits compile-time metaprogramming against Scala syntax trees, from within the Scala language.

774 questions
274
votes
1 answer

How to use Shapeless in a Quasiquote?

I'm trying to call a Shapeless macro from inside a quasiquote with Scala and I'm not getting what I would like to get. My macro doesn't return any errors but it doesn't expand Witness(fieldName) into Witness.Lt[String] val implicits =…
Roch
  • 21,741
  • 29
  • 77
  • 120
182
votes
1 answer

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

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…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
72
votes
1 answer

Documenting Scala 2.10 macros

I'll start with an example. Here's an equivalent of List.fill for tuples as a macro in Scala 2.10: import scala.language.experimental.macros import scala.reflect.macros.Context object TupleExample { def fill[A](arity: Int)(a: A): Product = macro…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
42
votes
1 answer

Static return type of Scala macros

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):…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
41
votes
6 answers

Iteration over a sealed trait in Scala?

I just wanted to know if it is possible to iterate over a sealed trait in Scala? If not, why is it not possible? Since the trait is sealed it should be possible no? What I want to do is something like that: sealed trait ResizedImageKey { /** *…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
40
votes
2 answers

Where can I learn about constructing AST's for Scala macros?

Where I can learn how to construct the AST's that Scala's macros generate? The Scaladoc isn't as helpful as I'd like. For example: abstract def Apply(sym: Universe.Symbol, args: Universe.Tree*): Universe.Tree A factory method for Apply nodes. But…
Bill
  • 44,502
  • 24
  • 122
  • 213
39
votes
2 answers

How to make IntelliJ IDEA recognise code created by macros?

Background I have an sbt-managed Scala project that uses the usual sbt project layout for Scala projects with macros, i.e., a subproject that contains the macros a main project that is the actual application and that depends on the macro subproject.…
36
votes
2 answers

Scala macros and the JVM's method size limit

I'm replacing some code generation components in a Java program with Scala macros, and am running into the Java Virtual Machine's limit on the size of the generated byte code for individual methods (64 kilobytes). For example, suppose we have a…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
34
votes
3 answers

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

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…
Emre
  • 1,023
  • 2
  • 9
  • 24
29
votes
1 answer

Matching function literals with quasiquotes in Scala

This question is similar in motivation to my previous question (although it's about a problem I ran into in a different context). I can pattern match on a function literal pretty easily without quasiquotes: import scala.reflect.macros.Context import…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
23
votes
2 answers

Howto model named parameters in method invocations with Scala macros?

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:…
Daniel Dietrich
  • 2,262
  • 20
  • 25
22
votes
2 answers

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

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…
Travis Brown
  • 138,631
  • 12
  • 375
  • 680
21
votes
1 answer

Getting subclasses of a sealed trait

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?
NietzscheanAI
  • 966
  • 6
  • 16
15
votes
1 answer

Scala macros: What is the difference between typed (aka typechecked) and untyped Trees

I'm getting started with scala macros, they're awesome, but I'm running into the difference between typed (aka typechecked) and untyped Trees. For example, you can't call c.eval with a typechecked Tree for some reason. I can't find documentation on…
Jaap
  • 3,081
  • 2
  • 29
  • 50
14
votes
1 answer

"dynamically" creating case classes with macros

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…
user833970
  • 2,729
  • 3
  • 26
  • 41
1
2 3
51 52