简体   繁体   中英

Scala - are classes sufficient?

Coming from Java I am confused by the class/object distinction of scala. Note that I do not ask for the formal difference; there are enough references on the web which explain this, and there are related questions on SO .

My questions are:

  1. Why did the designers of scala choosed to make things more complicated (compared to Java or C#)? What disadvantages do I have to expect if I ignore this distinction and declare only classes?

Thanks.

Java classes contain two completely different types of members -- instance members (such as BigDecimal.plus ) and static members (such as BigDecimal.valueOf ). In Scala, there are only instance members. This is actually a simplification! But it leaves a problem: where do we put methods like valueOf ? That's where objects are useful.

class BigDecimal(value: String) {
   def plus(that: BigDecimal): BigDecimal = // ...
}

object BigDecimal {
   def valueOf(i: Int): BigDecimal = // ...
}

You can view this as the declaration of anonymous class and a single instantiation thereof:

class BigDecimal$object {
   def valueOf(i: Int): BigDecimal = // ...
}
lazy val BigDecimal = new BigDecimal$object

When reading Scala code, it is crucial to distinguish types from values. I've configured IntelliJ to hightlight types blue.

val ls = List.empty[Int]  // List is a value, a reference the the object List
ls: List[Int]             // List is a type, a reference to class List

Java also has another degree of complexity that was removed in Scala -- the distinction between fields and methods. Fields aren't allowed on interfaces, except if they are static and final; methods can be overriden, fields instead are hidden if redefined in a subclass. Scala does away with this complexity, and only exposes methods to the programmer.

Finally, a glib answer to your second question: If you don't declare any objects, you're program may never run, as you to define the equivalent of public static void main(String... args) {} in Scala, you need at least one object!

Scala doesn't have any notion of static methods with standard classes, so in those scenarios you'll have to use objects. Interesting article here which provides a good intro:

http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-3

(scroll down to Scala's Sort-of Statics)

One way to look at it is this. An executing program consists of a community of objects and threads. Threads execute code within the context of objects -- ie there is always a "this" object that a thread is executing within. This is a simplification from Java in the sense that in Java, there is not always a "this". But now there is a chicken/egg problem. If objects are created by threads and threads are executed within objects, what object is the first thread initially executing within. There has to be a nonempty set of objects that exist at the start of program execution. These are the objects declared with the object keyword.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM