Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have been studying Scala, but what I keep running into is the optimization of syntax. I'm sure that will be great when I am an expert, but until then.. Not so much.

Is there a command or a program that that would add back in all/most of the code that was optimized away? Then I would be able to read the examples, for example.

share|improve this question
    
Related stackoverflow.com/questions/9891407/… –  om-nom-nom Dec 2 '12 at 20:40
add comment

1 Answer

You can use -print when compiling Scala, and it will print a de-sugared version of the code. Unfortunately, you'll see that Scala has syntactic sugar for good reason.

Let's see a very simple example:

object T {
  def first[A : Ordering](l: List[A]): A = {
    val s = l.sorted
    s(0)
  }
}

And the output:

package <empty> {
  final object T extends java.lang.Object with ScalaObject {
    def first(l: List, evidence$1: scala.math.Ordering): java.lang.Object = {
      val s: List = l.sorted(evidence$1).$asInstanceOf[List]();
      s.apply(0)
    };
    def this(): object T = {
      T.super.this();
      ()
    }
  }
}

You'll often see things like <empty> -- these are compiler annotations, not valid Scala code. For example, a parameter in a case class will have its getter tagged with <stable> <caseaccessor> <accessor> <paramaccessor>. Also, a case class will have a number of methods marked <synthetic>, to indicate they were created by the compiler.

Also, the this constructor you see here is not valid Scala code, exactly. In this case, the "normal" Scala constructor (which is the body of the class, trait or object) is moved inside such a this method -- which is how Java itself sees things.

So, this is mixture of Scala and Java, with a bit of compiler internals sprinkled over it. But it will show the de-sugared stuff.

share|improve this answer
    
cool, i'll have to play with that. Maybe it will help with examples from the books I can't follow. –  Paul Aug 12 '11 at 1:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.