All Questions
406
questions
181
votes
1
answer
9k
views
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 ...
72
votes
1
answer
2k
views
Documenting Scala 2.10 macros [closed]
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 {
...
53
votes
0
answers
1k
views
Materialize the Value for a Type that has One Inhabitant
Thanks to @MilesSabin's answer I can write a type level Fibonacci sequence:
sealed trait Digit
case object Zero extends Digit
case object One extends Digit
sealed trait Dense { type N <: Dense }
...
42
votes
1
answer
4k
views
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: ...
41
votes
2
answers
5k
views
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*): ...
38
votes
2
answers
10k
views
What's the easiest way to use reify (get an AST of) an expression in Scala?
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 ...
36
votes
2
answers
4k
views
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 (...
29
votes
1
answer
3k
views
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 ...
19
votes
2
answers
4k
views
How do I print an expanded macro in Scala?
I am writing a macro in Scala, but when I call it I get an error message saying "Double does not take parameters". Clearly there is something wrong with how the macro builds the AST. So how can I see ...
17
votes
2
answers
931
views
Using private constructor in a macro
I want to use a private constructor in a macro. This example is a positive integer, but the basic pattern could not only be used for other numeric types like even numbers, but also string derived ...
16
votes
1
answer
6k
views
Scala macros, where are they used?
I just noticed that Scala has macros, but I have never seen any code that uses them. They also seem quite different from C preprocessor macros and the like. Reading through the overview of macros, it ...
15
votes
1
answer
4k
views
String interpolation and macro: how to get the StringContext and expression locations
I'm trying to implement a custom string interpolation method with a macro and I need some guidance on using the API.
Here is what I want to do:
/** expected
* LocatedPieces(List(("\nHello ", Place(...
14
votes
3
answers
3k
views
Avoiding boxing/unboxing within function
For a numeric intensive code I have written a function with the following signature:
def update( f: (Int,Int,Double) => Double ): Unit = {...}
However, because Function3 is not specialized, every ...
13
votes
1
answer
694
views
SBT Compiler Plugin as Transitive Dependency
I have a library that uses the macro paradise plugin (referred to as macro-provider library). In the build.sbt,
addCompilerPlugin("org.scalamacros" % "paradise" % "2.0.0" cross CrossVersion.full)
to ...
12
votes
2
answers
1k
views
Introspect argument passed to a Scala macro
I would like to program a Scala macro that takes an instance of a case class as argument. All objects that can be passed to the macro have to implement a specific marker trait.
The following snippet ...
12
votes
1
answer
1k
views
Method cannot be accessed in Macro generated class
I have the following macro defining a class and returning an instance of that class (with Scala 2.10.2 and the macro plugin):
def test[T] = macro testImpl[T]
def testImpl[T : c.WeakTypeTag](c: ...
11
votes
1
answer
2k
views
How can I reuse definition (AST) subtrees in a macro?
I am working in a Scala embedded DSL and macros are becoming a main tool for achieving my purposes. I am getting an error while trying to reuse a subtree from the incoming macro expression into the ...
11
votes
1
answer
1k
views
Nested implicit macros seems not working for type parameters?
I'm writing a Scala implicit macro that automatically generates a type class for case classes (using quasiquote, both Scala 2.10.3 with the macro paradise compiler plugin and Scala 2.11.0-M7).
The ...
11
votes
1
answer
274
views
How to distinguish triple quotes from single quotes in macros?
I am writing a macro m(expr: String), where expr is an expression in some language (not Scala):
m("SOME EXPRESSION")
m("""
SOME EXPRESSION
""")
When I am parsing the expression I would like to ...
10
votes
1
answer
3k
views
Use Scala macros to generate methods
I want to generate aliases of methods using annotation macros in Scala 2.11+. I am not even sure that is even possible. If yes, how?
Example - Given this below, I want the annotation macros to expand ...
10
votes
2
answers
640
views
Scala macros and separate compilation units
Is this limitation going to be overcome in the future? Or is there some way to get around this that I'm not aware of?
Right now I have a project where I would like to use macros, but they would need ...
10
votes
1
answer
723
views
Checking for varargs type ascription in Scala macros
Suppose I have this macro:
import language.experimental.macros
import scala.reflect.macros.Context
object FooExample {
def foo[A](xs: A*): Int = macro foo_impl[A]
def foo_impl[A](c: Context)(xs: ...
9
votes
4
answers
8k
views
Converting a case class to CSV in Scala
Is there a elegant way to convert a case class to a CSV value.
For example -
case class Person( name : String, age : Int, gender: String, address : Option[String])
I was thinking about using ...
9
votes
2
answers
622
views
Similarities and differences between Scala macros and C++ templates [closed]
Scala's Macros and C++ templates both provide access to compile time meta-programming. Could you elaborate on similarities and differences? Are they equal in terms of expressiveness?
9
votes
2
answers
899
views
Splicing a passed function body into a macro-rewritten expression
I was playing with Scala 2.11's new macro features. I wanted to see if I could do the following rewrite:
forRange(0 to 10) { i => println(i) }
// into
val iter = (0 to 10).iterator
while (iter....
9
votes
1
answer
4k
views
Scala Macros: "cannot create TypeTag from a type T having unresolved type parameters"
I'm playing around with Scalas new macros and found this gist from akshaal. As it seams I did not quite get it.
Given the following trait (the fieldsMacro is more or less the same as in akshaal ...
9
votes
1
answer
1k
views
How do I refer to enclosing "this" in a Scala macro?
The following macro, extracted from a larger example, is supposed to create a tree with nothing but a reference to this:
def echoThisImpl(c:Context): c.Expr[Any] = {
import c.universe._
val ...
9
votes
1
answer
805
views
Is it possible to define a macro with variadic parameters, and get a type for each parameter?
The following is an obvious variadic function:
def fun(xs: Any*) = ???
We can define a macro in a similar way:
def funImpl(c: Context)(xs: c.Expr[Any]*) = ???
fun(1,"1",1.0)
But in this case, ...
9
votes
1
answer
334
views
How do I distribute a Scala macro as a project?
Suppose I have a Scala compile-time macro that I find useful and would like to share it (I do). How do I create a JAR file that when loaded into another project would execute the macro when compiling ...
9
votes
1
answer
2k
views
Quasiquotes for multiple parameters and parameter lists
Quasiquotes are amazing—they make writing macros in Scala hugely less painful, and in my experience they almost always just work exactly as I'd expect. And best of all, they're now available as a ...
9
votes
3
answers
339
views
What's the recommended way to pass the results of macro computations to run-time?
I'm trying to build some SQL-like abstraction and I have hit a problem.
This is a simplified “database table”:
trait Coffee {
def id: Long
def name: String
def brand: String
}
This is my ...
8
votes
2
answers
937
views
Scala 2.10 macros compared to what is available in Java
I don't know this field so much.
Can someone explain what is possible in Scala 2.10 with macros, compared to what is possible in Java with compilation preprocessors, and tools like CGLIB, ASM, ...
8
votes
1
answer
2k
views
Can't access Parent's Members while dealing with Macro Annotations
I am kind of blocked with the following (macro annotation)
situation. Suppose I have an annotation called @factory which
aims to generate an apply method for the annotated trait in the corresponding ...
8
votes
2
answers
586
views
Macro to access source code text at runtime
Is there already or is it possible to have a Scala macro that gives me access to the text of the source? For instance I would like to write code like this:
val list = List(1, 2, 3)
val (text, sum) = (...
8
votes
1
answer
2k
views
Scala Macros: Getting a List of TypeSymbols to be used at runtime
Is there a way to return a List of TypeSymbols for each class under a package using macros?
What I am trying to achieve is to write a macro that gives out something equivalent to this list:
scala>...
8
votes
1
answer
468
views
Will it be possible to generate several top-level classes with one macro invocation in scala 2.10?
I have an program with lots of boilerplate (which is, sadly, not reducible even by Scala mechanisms). But if there would be a way to generate complex top-level classes with a macro, all that ...
8
votes
2
answers
1k
views
Creating a method definition tree from a method symbol and a body
Is there a convenient way to turn a MethodSymbol into the left-hand side of a method definition tree (i.e., a DefDef) in Scala 2.10?
For example, suppose I want to create a macro that will take an ...
8
votes
1
answer
2k
views
How to check which parameters of case class have default value using scala reflection 2.10
My task is to find names of parameters of case class, for which there are default values.
In 2.9 I was using ScalaSigParser from scalap and did something similar to:
(...)
case x: MethodSymbol if x....
8
votes
1
answer
559
views
Scala 3 (Dotty) Pattern match a function with a macro quotation
I'm trying to get the function name via macros in Scala 3.0.0-M2
The solution that I came up with uses TreeAccumulator
import scala.quoted._
inline def getName[T](inline f: T => Any): String = ${...
7
votes
2
answers
938
views
Infer HList type when building the list with a macro
I have a method taking an HList and using it to build an instance of a class.
I would like to provide some simplified syntax, hiding the explicit cons. So I'd like to go from:
MyThingy.describe( 42 ::...
7
votes
1
answer
1k
views
Instantiate a class with Scala Macro or reflection
On my scala code, I want to be able to instantiate a new class. For instance, supose I have the code below:
class Foo { def foo=10 }
trait Bar { val bar=20 }
Ideally, I want to be able to do ...
7
votes
4
answers
18k
views
How can I create an instance of a Case Class with constructor arguments with no Parameters in Scala?
I'm making a Scala app that sets by reflection field values. This works OK.
However, in order to set field values I need a created instance. If I have a class with an empty constructor, I can do this ...
7
votes
1
answer
359
views
Is it possible to implement something akin to Scala's @BeanProperty with macros?
I would like to create an annotation or trait that adds methods to an object at compile time dynamically, based on existing fields. Although I'm interested in something at the class-level, I'd also ...
7
votes
2
answers
3k
views
Scala Macros: Accessing members with quasiquotes
I'm trying to implement an implicit materializer as described here: http://docs.scala-lang.org/overviews/macros/implicits.html
I decided to create a macro that converts a case class from and to a ...
7
votes
1
answer
921
views
Finding implicit method definitions in macro context
I understand the basic concept of macros in Scala, but currently fail to do this (simple?) job:
Find all implicit defs/vals currently visible to the compiler to transform from a given type to another ...
7
votes
1
answer
2k
views
Create or extend a companion object, using a macro annotation on the class
Using a Scala 2.10/2.11 macro paradise annotation macro, how can I add or extend the companion object of an annotated class? Skeleton:
import scala.annotation.StaticAnnotation
import scala.reflect....
7
votes
1
answer
432
views
Imports and wildcard imports of Symbols in Scala
I have a list of Symbols representing packages, objects and classes and want to import them in a macro context.
For packages and objects, this would mean a wildcard import and for classes it would ...
6
votes
2
answers
1k
views
Infer a type of a tree in a scala macro
Inside a macro, how can I ask the compiler to infer the type of a constructed tree?
I've only found Context.typeCheck, but that only checks the types but doesn't return the result.
6
votes
3
answers
806
views
What's the purpose of macros?
Here my general focus is Scala and Lisp/Scheme macros, not exactly the ones in the C/C++/Obj-C
I just don't see the point.
The way I understand it, is that macros are there to extend the language. ...
6
votes
1
answer
2k
views
How do I create a class or object in Scala Macros?
I'm attempting to generate a companion object from a case class using Macros but I'm having a very hard time finding any examples of how to accomplish this.
For example:
case class Person(name: ...