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 the expanded macro? Is there a way to call the macro implementation at runtime?
Provide -Ymacro-debug-lite or -Ymacro-debug-verbose option to the compiler.
Off the top of my head, detalization of printed ASTs is governed by -Yshow-trees-compact, -Yshow-trees-stringified, -Xprint-types, -uniqid and -Yshow-symkinds. You can find other gems by running scala -X and scala -Y (or inspecting the sources of scala settings at https://github.com/scala/scala/blob/2.10.x/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala).
Also, despite being essentially a macro, reification has its own tracing mechanism that can be configured by -Yreify-copypaste and -Yreify-debug.
There is Macrocosm's desugar which can show how your source code, including, but no limited to macros, is transformed into.
println("TRANSFORMATION:\n"+ desugar{
println("a string")
MY_MACRO("something")
})
Related
We have problems when using Scalac -Xfatal-warnings in the following cases:
Implicit vals used by macros internally
Internal vals that macros auto-generate
In both cases, we see Scalac failing to compile because it detects some values are not used, while we know they are (simply, when we remove them, the code doesn't compile anymore)
Although the two might be symptoms of the same problem in Scalac, they boil down to the same issue for us: we need to disable the -Ywarn-unused in Scala 2.11.12
Is there a way to exclude specific class files so they won't be affected by the compiler flag?
As far as I know there is no way of disabling scalac flag for just one file (if you compile your whole project at once by e.g sbt). You can extract class into separate module with different compile flags.
In case of implicit vals used internally in macros, personally I use -Ywarn-macros:after flag, which make these implicits used in macro count as used. (Talking about Scala 2.12.4).
I'm using a library that makes heavy use of macros and applyDynamic. The compiler bug that prevents using varargs with applyDynamic has been causing me a lot of pain, and I'm hoping to find a workaround that allows me to pass a dynamic sequence of arguments into an applyDynamic that then invokes a macro.
I have tried to write a macro that expands the sequence or splat before invoking applyDynamic with little success, but I'm not very comfortable with Scala macros so I might have been doing something wrong.
I hit this problem while using ScalaJS on this line:
literal(seq: _*)
the workaround was simply this:
literal.applyDynamic("apply")(seq: _*)
Explanation: literal (more precisely: scala.scalajs.js.Dynamic.literal) is an object extending scala.Dynamic and the call literal(...) is desuggared to literal.apply(...), which itself should be transformed (if there were no bug) by the compiler to literal.applyDynamic("apply")(...). Well, it's a bit ugly, but one can always remove the sugar himself.
This issue has been fixed since Scala 2.12.5
I know programmers are supposed to wrap their code in an application object:
object Hello extends App {
println("Hello, World")
}
It is required in Eclipse, if I ever want to get any output. However, when I tried to write some code (very casually) in Emacs, I write like this:
class Pair[+T](val first: T, val second: T)
trait Friend[-T] {
def befriend(someone: T)
}
def makeFriendWith(s: Student, f: Friend[Student]) {
f.befriend(s)
}
It seems like there is no universal object or class that wraps over the function makeFriendWith. Is Scala like JavaScript, everything is attached to a global object? If not, what is this function attached to?
Also why can this work in console (I complied it with scala command and it worked) but does not work in Eclipse? What's the use of the Application object?
Scala doesn't have top-level defs, but your script can be run by either the REPL or the scala script runner.
The precise behavior of your script depends on which way you run it.
The REPL can run scripts line-by-line or whole hog. (Compare :paste and :paste -raw versus :load or -i init.script and the future option -I init.script.)
There is an issue about sensitive scripting. The script runner should realize if you're trying to run an App.
There is another effort to make scripting a compiler phase that is easily customized. Scroll to Scripter.scala for code comments about its current heuristics.
In short, your defs must be wrapped in a top-level entity, but exactly how that happens is context-dependent.
There was a recent effort to make an alternative baked-in wrapping scheme available for the REPL.
None of this is mandated by the language spec, any more than special rules pertaining to sbt build files are defined by the language.
You can define methods like this only in the console, which (behind the scenes) automatically wraps them in an anonymous class for you.
Outside of the console, there's no such luxury.
As a JVM language, Scala cannot truly create any top-level entities other than classes and interfaces.
It does, however, have the notion of a "package object" which creates the illusion of value entites (val, var and def) not enclosed in a class or trait.
See http://www.scala-lang.org/docu/files/packageobjects/packageobjects.html for information on package objects.
You can run code like this directly in Eclipse: use Scala worksheet. IntelliJ IDEA Scala plugin supports it as well.
What is the difference between a scala script and scala application? Please provide an example
The book I am reading says that a script must always end in a result expression whereas the application ends in a definition. Unfortunately no clear example is shown.
Please help clarify this for me
I think that what the author means is that a regular scala file needs to define a class or an object in order to work/be useful, you can't use top-level expressions (because the entry-points to a compiled file are pre-defined). For example:
println("foo")
object Bar {
// Some code
}
The println statement is invalid in the top-level of a .scala file, because the only logical interpretation would be to run it at compile time, which doesn't really make sense.
Scala scripts in contrast can contain expressions on the top-level, because those are executed when the script is run, which makes sense again. If a Scala script file only contains definitions on the other hand, it would be useless as well, because the script wouldn't know what to do with the definitions. If you'd use the definitions in some way, however, that'd be okay again, e.g.:
object Foo {
def bar = "test"
}
println(Foo.bar)
The latter is valid as a scala script, because the last statement is an expression using the previous definition, but not a definition itself.
Comparison
Features of scripts:
Like applications, scripts get compiled before running. Actually, the compiler translates scripts to applications before compiling, as shown below.
No need to run the compiler yourself - scala does it for you when you run your script.
Feeling is very similar to script languages like bash, python, or ruby - you directly see the results of your edits, and get a very quick debug cycle.
You don't need to provide a main method, as the compiler will add one for you.
Scala scripts tend to be useful for smaller tasks that can be implemented in a single file.
Scala applications on the other hand, are much better suited when your projects start to grow more complex. They allow to split tasks into different files and namespaces, which is important for maintaining clarity.
Example
If you write the following script:
#!/usr/bin/env scala
println("foo")
Scala 2.11.1 compiler will pretend (source on github) you had written:
object Main {
def main(args: Array[String]): Unit =
new AnyRef {
println("foo")
}
}
Well, I always thought this is a Scala script:
$ cat script
#!/usr/bin/scala
!#
println("Hello, World!")
Running with simple:
$ ./script
An application on the other hand has to be compiled to .class and executed explicitly using java runtime.
According to "Programming in Scala" one can pass the argument -Xprint:typer to the compiler and gets the code back as it looks after all the implicites are actually applied.
I also found that I can set compiler arguments in the project properties.
But I can't find any resulting output anywhere ...
So where do I have to look?
If you start Eclipse from a console, you should see the printed output there.
With the Scala IDE 2.1 Milestone 1 you can press Ctrl-1 to make implicits explicit
From http://scala-ide.org/download/milestone.html#scala_ide_21_milestone_1
Highlight Implicits It has never been easier to know where implicits
are applied. And, by pressing Cmd/Ctrl+1, turn an implicit conversion
into an explicit call!