问题标签 [scala-macros]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
293 浏览

scala - 如何区分编译器推断的隐式转换和显式调用的转换?

让我们想象一下将这两个等价的表达式传递给 Scala 宏:

  • 使用编译器推断的隐式转换:1+"foo"
  • 显式调用隐式转换:any2stringadd(1)+"foo"

有没有办法在宏内部区分这两者?

0 投票
1 回答
384 浏览

class - Scala 中符号的导入和通配符导入

我有一个表示包、对象和类的符号列表,并希望在宏上下文中导入它们。

对于包和对象,这意味着通配符导入,而对于类,这意味着“标准”导入。

给定由和List[Symbol]组成的some.package,我将如何正确导入它们以及如何决定是否需要使用“标准”或通配符导入?some.Classsome.Object

我目前的做法是这样的:

包/模块导入有效,但类导入不起作用,尽管它看起来正确。

0 投票
1 回答
776 浏览

scala - 在 selectDynamic 的宏实现中调用实例方法

我想Sample#cap(String)在下面的宏实现中使用 selectDynamic。

可能吗?

0 投票
1 回答
445 浏览

scala - 在编译时使用宏创建类型良好的实例

我希望能够使用基于某些类名的宏创建类型良好的实例。例如,我希望能够Long像这样创建一个实例:

实现将类似于:

我的问题是:

  • 鉴于类型取决于参数,我需要做什么才能让宏返回正确类型的对象?

  • 甚至可以使用 Scala 2.10 吗?

0 投票
1 回答
200 浏览

scala - 在 Scala 2.10.1 中找不到隐式宏?

似乎该implicit关键字在应用于宏时不起作用def

例如,采用以下代码:

失败,但implicitly[Foo[Int]]不应该;如果我替换implicit def implicitFoo[A]: Foo[A] = macro implicitFoo_impl[A]implicit def implicitFoo[A]: Foo[A] = ???,编译成功。

这是错误还是用户错误?

0 投票
1 回答
276 浏览

scala - 如何检查 WeakTypeTag 或 Type 是否代表具体类型?

如何检查WeakTypeTagType表示具体类型?这在宏中特别有用,当用户给出的类型不具体时,我可以使用它来引发编译错误:

0 投票
1 回答
213 浏览

scala - 使用反射或宏时如何可靠地比较符号?

如何Symbol在 scala 宏中或使用反射时可靠地比较两个 s 是否相等?是否保证当两个Symbol对象表示相同的符号(相同的类、相同的局部值或变量等)时,它们在==运算符方面是相等的?

我需要这个做什么?我有Tree一个宏,它代表一些可能引用一些本地值的表达式。我想转换这棵树并将对这个本地值的所有引用替换为对其他值的引用。

0 投票
1 回答
133 浏览

scala - 如何具体化符号以将其传递到运行时?

Scala 中的宏上下文有两个方便的方法:它们本质reifyTypereifyTree生成代码,当在运行时执行时,将返回Type或被Tree具体化。

我想知道是否有某种方法可以实现与Symbols 类似的东西 - 某种reifySymbol方法?

0 投票
2 回答
760 浏览

scala - How to debug Scala Macros using Eclipse

I am trying to set a breakpoint in a Scala Macro implementation using the Eclipse IDE and failing

Firstly: Scala Macros Rock! Up to now I have preferred Clojure to Scala, but with macros I'm no longer sure

I'm trying to create a macro that will return the toString of a function and the function itself. When that works I'm going to make a new function with a sensible toString. Ah happy days.

But I need to be able to debug the macros. I use Eclipse (20110615-0604), with Scala (2.10.1). I downloaded the scala-compiler-2.10.1.jar and the code from http://www.warski.org/blog/2012/12/starting-with-scala-macros-a-short-tutorial/ now works. I've written a couple of simple macros as well. The macros are in an eclipse project "ScalaMacro" and the code that uses them is in a separate project "HelloScalaMacro"

I'd now like to debug them

Following the instructions at http://docs.scala-lang.org/overviews/macros/overview.html I have created a runtime configuration with scala.tools.nsc.Main as the entry point. I've added -cp HelloScala.scala, and when I run the configuration it actually seems to compile the code (if I put errors in, it reports the errors correctly).

Unfortunately the instructions imply that a breakpoint in the macro implementation should cause Eclipse to pause. It doesn't.

I've done the usual: google search for Eclipse/Scala macro/Debug/Breakpoint, read all the stackoverflow questions in the scala-macro tag, and played around a lot with every eclipse setting I can find.

So if any of you out there know how to set breakpoints, could you let me know how: is it an eclipse version / scala version / ... issue?

0 投票
1 回答
2170 浏览

scala - 如何获取 scala 宏来替换方法调用

如何获取 scala 宏来替换方法调用?

我的目标是创建一个名为ToStringAdder. 假设我有一个x具有此特征的对象,那么当我调用时,x.add(any)我希望宏实际调用x.add(any, string)字符串是 AST 的字符串表示形式的位置。(这样tostring当'any'是一个函数时我可以很好)。

除了 Expecty,我看到的所有这些例子都有效地使用了静态方法调用:调用宏的对象没有被使用。Expecty 具有以下方法,它为我提供了有关如何检测“隐式 this”的线索,但我找不到在 reify 调用中引用它的方法。

那么我该如何替换对已调用宏的对象的调用。我现在的代码如下,需要排序的是reify调用中的代码