Scala - are classes sufficient?

Coming from Java, I am confused by the difference of the scala class / object. Notice that I am not asking for a formal difference; there are enough links on the Internet that explain this, and there are related questions on https://stackoverflow.com/a/4648/2 .

My questions:

  • Why did the designers from scala choose to make it more complicated (compared to Java or C #)? What disadvantages do I have if I ignore this distinction and declare only classes?

Thanks.

+7
scala class
source share
3 answers

Java classes contain two completely different types of members - instance members (e.g., BigDecimal.plus ) and static members (e.g., BigDecimal.valueOf ). Scala has members only . This is actually a simplification! But that leaves a problem: where do we put methods like valueOf ? 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 a declaration of an anonymous class and one copy of it:

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

When reading Scala code, it is important to distinguish between types and values. I configured IntelliJ to hightlight blue types.

 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 has been removed in Scala - the distinction between fields and methods. Fields are not allowed on interfaces, unless they are static and finite; methods can be overridden, fields are hidden instead if they are overridden in a subclass. Scala eliminates this complexity and only provides methods to the programmer.

Finally, glib's answer to your second question: if you do not declare any objects, you can never run the program, since you must define the equivalent of public static void main(String... args) {} in Scala, you need though would be one object!

+23
source share

Scala has no concept of static methods with standard classes, so you will have to use objects in these scripts. An interesting article here that gives a good introduction:

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

(scroll down to Scala s Sort statistics)

+2
source share

One way to look at it. A running program consists of a community of objects and threads. Threads execute code in the context of objects - i.e. There is always an "this" object in which the thread executes internally. This is a simplification from Java in the sense that Java does not always have "this". But now there is a problem with the chicken / egg. If objects are created by threads and threads are executed inside objects, then which object is the first thread that is initially executed inside. There must be a non-empty set of objects that exist at the beginning of program execution. These are objects declared using the object keyword.

0
source share

All Articles