Take the 2-minute tour ×

Why is the "abstract" keyword for class definition optional in Scala, and how do I force the Scala compiler to tell me when my class is abstract?

Here an example that I wrote in Eclipse:

class Toto[T] {
    def get(index: Int): T

object Toto {
    def create[T]: Toto[T] = new Toto[T]

This seems to be a perfectly valid class definition in Scala, although it does NOT define the required get method, and is NOT prefixed with abstract. If you don't need the abstract keyword, then why does it exist? And if you want to be told that your class is actually abstract, how do you get the compiler to tell you?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

This is not valid scala code, abstract is required, and instanciation forbidden. From the spec (5.2, p63):

The abstract modifier is used in class definitions. It is redundant for traits, and mandatory for all other classes which have incomplete members. Ab- stract classes cannot be instantiated (§6.10) with a constructor invocation unless followed by mixins and/or a refinement which override all incomplete members of the class. Only abstract classes and traits can have abstract term members.

The code produces an error in the REPL : error: class Toto needs to be abstract, since method get is not defined

I get the proper behavior with the same message in Eclipse too. You should check whether you get the same error with and without eclipse. Whichever is true, I guess if you have exactly the code you posted without an error (does it run?), a bug repport will be warranted.

share|improve this answer
So it would be an Eclipse bug? Well, I certainly would prefer that. I haven't checked if it runs, but I do have the exact same code in a file on it's own in Eclipse, with build automatically activated, and it does NOT give any error. I have some unrelated class that does not compile. Maybe the compiler stops before compiling all classes because of too many errors? –  Sebastien Diot Jun 22 '11 at 8:04
That sounds quite possible. You should at least check whether in a clean project, you get an error with that code. Or maybe force a rebuild with Project/Clean –  Didier Dupont Jun 22 '11 at 8:13
I commented out the class that gave me errors, and then suddenly Eclipse found a dozen errors in other classes that looked OK until now. –  Sebastien Diot Jun 22 '11 at 8:24
add comment

To answer my own question: In Eclipse, you can only tell if a class is correct if all other classes compile without errors! In other word, you can't trust anything Eclipse says about a class unless there are no errors in other classes.

So if you have errors in several classes, then there is no way of knowing which ones are the real errors, and neither if a class without errors is correct.

You just have to repeatedly loop on the errors, fixing any one that makes sense, and hoping the others errors that don't make sense are eventually going to just disappear.

share|improve this answer
According to my experience, you can trust all errors that the compiler reports (when you choose Project -> Build project). The presentation compiler (active in the editor while you're typing) can have the problem you describe. –  Jean-Philippe Pellet Jun 22 '11 at 8:37
add comment

Your Answer


