My problem is understanding is the syntax of Scala. I come from a Java background. I am trying to make a variable of the same type as the class it is in. Example:
class Exp {
var exp1: Exp;
}
I am getting this error:
Driver.scala:4: error: class Exp needs to be abstract, since variable exp1 is not defined
(Note that variables need to be initialized to be defined)
class Exp {
Can someone explain why I cannot do this? I am new to the language. Any explanation would help in understanding it better.
Because you need to initialize it. Otherwise the compiler thinks you want only the variable’s interface: the getter and setter methods. It’s very similar to how a method without a body is abstract. The following will initialize it to null and give you a valid concrete class with a concrete variable.
This use of _ means “default value” where the default is null for reference types and 0, false, or something similar for non-reference types.