Sebastien Diot
Sebastien Diot

Reputation: 7359

Explain strange Scala compiler behaviour

I am working with Scala 2.9.0.1 in Eclipse.

I have a Scala class/trait hierarchy, with something like:

A.scala: trait A
B.scala: trait B extends A
C.scala: trait C extends B

Except more complicated; too big to post as example.

So, C depends indirectly on A, but A does not know anything about C.

Now if I change A, I get error everywhere, so I work my way up. Once I finally fixed the last error in C, suddenly the compiler tells me that A cannot be compiled. Since A does not in any way depend on C, this makes no logical sense. In fact I would say it is a compiler bug. This happened to me several times now, and it is very frustrating, because it means I have to change A again, and modify my whole class hierarchy again.

Is there any way to get the Scala compiler (in Eclipse) to give me reliable results? Am I the only one that gets this behavior?

Upvotes: 0

Views: 195

Answers (1)

Kipton Barros
Kipton Barros

Reputation: 21112

I have also observed that a Scala program can have "hierarchies" of errors, where certain compile errors don't get revealed until others are fixed. This isn't necessarily a compiler bug (Others say this is likely a bug with the old Scala IDE builder that's been fixed in trunk.)

One strategy is to make the changes incrementally:

  1. Make a copy of trait A and call it trait A2.
  2. Make the desired changes to A2, fixing any compile errors that show up. This is easy since nothing depends on A2 yet.
  3. Repeat for B2, C2, etc.

Once everything is working, replace the original traits with the modified ones. Revision control (I use git) can be very helpful in this process.

Another general tactic that I find useful is to divide the design into two stages:

  1. First, I code just the types, leaving all the method bodies empty. It's much easier to fix type errors before there's any real code in the way.

  2. Once the types are consistent, I can fill in the method definitions independently without worrying about complicated compile errors.

Upvotes: 2

Related Questions