How do I get the log output of a Scala Compiler Plugin? - scala

From the scala compiler help, I would have thought that "-Xprint:MyPhase" would do just that, but despite the fact that the plugin does it's job, and that I know it makes log output, I still don't see any output from coming from scalac when it runs. All I see are "[[syntax trees at end of MyPhase]] ..." Since logging seems to simply involve calling log("..."), I can't imagine what I would be doing wrong in the code that produces the log output. As an ugly work-around I can still do System.out.println(), but if there is a log() method, than I have to assume that it's actually meant to do something ...

-Ylog:<phase>
Use scalac -Y to see all the "private" options.

Related

println in scala using Intellij showing weird behaviour

I'm writing a little program in scala that would simulate a doctors practice. One of the requirements is that a user can set consultations with a doctor. I have implemented a solution that gives you the consultation object if you succeed and also prints a message saying that the action was successful.
This is where a little "bug" (more a minor but annoying inconveniece) appears. When I tell my scala worksheet to make a new variable like so:
var consultation: Consultation = patient.makeConsultation(x, y, z)
Well this works perfectly BUT the message that it should print is printed at the top of the output window and not at the point in the worksheet were the variable is actually created. Does anyone know a solution for this?
Thanks in advance.
This is not a bug but the expected behaviour. The println in your makeConsultation function is a side-effect which has nothing to do with the creation of Consultation.
You can use Scala's type system (check Option or Try) to represent that makeConsultation could fail.

How to skip error checking when compiling Scala files in IDEA?

The run config option Make, no error check in IntelliJ IDEA does not skip Scala errors that result in ambiguous ClassDefNotFound errors upon running a project.
How do I skip error checking when compiling Scala files?
My end goal is for the program to fail at runtime when the classes with errors are accessed, not before runtime preventing the whole program from running.
As the name suggests, "Make, no error check" will just pretend compile errors don't matter. Then obviously you end up with an incomplete class path, not containing any classes that produce compiler errors. As a consequence, when you run that project, you should not be surprised to find class-not-found errors.
I don't know how this "feature" interacts with the incremental Scala compiler. I wouldn't expect "Make, no error check" to produce any useful results.
If you want to skip parts of your code that currently don't compile, a good solution is to use the ??? "hole" value, i.e. a placeholder of type Nothing that you can plug into any position where you would have incomplete code. E.g.
def myMethod: String = ??? // implement later
This allows these things to be compiled and will only produce runtime errors when hit during runtime.

System.err.println shows no output

I am using STS 3.4 and working on a web application based on Grails framework.
When i try to use System.err.println in groovy classes it does not print anything on standard eclipse console(STS console).
Actually there are times when in print things but that is like 1 in 10, I couldn't understand this random behavior.
I am using some library that uses System.err.println for debugging purposes but i could not get any debugging info. All i need to know is where and how to get System.err.println output?
Please help me, Thanks in advance
If it is a random behaviour, it may be not-flush-ed buffered stream. Especially, that can happen when output comes from different thread.
As a solution, you can hook in into System.err dispatching (it's a stream, that you can set from outside), and overload functions, to get desired output anywhere you want. Or simply force flush it. But be careful, as it may lead to performance problems.
Consider using logging instead for more standard and configurable output. This should help you to set it up: http://groovy.codehaus.org/Logging

How do you get the Scala IDE presentation compiler to show you the log output of a compiler plugin?

When I call the Scala compiler from the command line, using my own compiler plugin, I use the following two parameters to see what happens:
-Ylog:generatewrappers -Xprint:generatewrappers
This gives me all the information I need. Using the same plugin inside the Scala IDE for Eclipse, those options are not yet available in the compiler properties. If I just put them exactly as above in the "Additional command line parameters" field, it does not seem to have any effect, although I can see that the plugin is doing it's job.
So how can I get Eclipse to show me the output of the plugin/compiler, either in the Eclipse Console View or in the Problem View (or is there a special other View for this)?
EDIT: If anybody cares, I'm getting the very useful error message:
Error in Scala compiler: null
which is extremely frustrating. I tried using the -Ypresentation-log option, hoping to at least get the output in that file, but all it contains is something like this:
"wait for more work"(
),
"atnode"(
1,
1319373203925),
"asked"(
),
"atnode"(
2,
1319373203926),
...
"exception thrown"(
"scala.tools.nsc.interactive.ShutdownReq$"(
))
This error does not come when I compile from the command line using the same options, so I have no way of debugging it without the actual presentation compiler output.
All compiler log messages are printlns. They go to stdout. Run Eclipse from the command prompt to see them.

How to determine if a scala module is run as script

How do I determine if a Scala module is opened as script or if it is regularly imported?
This question is about the same issue as previous Python question:
how do I determine whether a python script is imported as module or run as script?
but for Scala
If you just need a quick hack for personal use, you can launch the Scala interpreter with a shell script that also sets an environment variable indicating that the interpreter is running.
Also, keep in mind that there's a difference between Scala and Python that makes the question somewhat moot: In Scala, code expressions can't appear at the top level, unless it's a Scala script. So you'll never really have the ambiguity of writing a Scala script and then wondering if it's being executed some other way.
All Scala modules are imported regularly, because there is no such thing as opening as a script.
Scala doesn't have the equivalent of Python's __main__ as far as I know so it can't be done in the same way. But I argue you shouldn't be doing this anyway - just write tests for your module or write a script that imports the module.
A dirty hack could be to throw and exception and get the trace. You can then examine it to try to guess the context. For example a method like:
def stackTrace: Array[StackTraceElement] =
try {
1/0 // cause exception
Array() //Never executed
} catch {
case e: Exception => e.getStackTrace
}
Will return the full stack trace as an array of StackTraceElement.
However, writing the trace analysis code will be tedious and I don't see any situation where it may be worth of it...