📜 ⬆️ ⬇️

About Scala for those who lack Java, and not only

Progress does not stand still, people are looking for new solutions, and more and more interesting languages ​​appear on the JVM. But the “core” of the JVM community is a stern people, accustomed to serious standards, and with high demands. Therefore, the majority of new languages ​​and hang "on the periphery."

Scala differs from other languages ​​on the JVM with a really solid approach - the laboratory of the most powerful European institute EPFL , headed by Professor Martin Oderski, who is also known as a designer of Java 1.5 generics, works on the language. Of course, this cannot be compared in terms of the amount of support with serious commercial offices like the Sun or Microsoft, so the language developed slowly and “went to work” quite recently:
scala Job Trends graph

What is interesting in it?

First of all, the familiar syntax and availability of Java baggage and free mixing with it - you can safely add several Scala classes to an existing Java project and everything will work (Scala has extensions inaccessible from Java, but it's usually not difficult to solve, in the opposite side everything is 100% accessible).
')

Concise and expressive code


Starting from the simplest type inference options and syntax breaks:
  def toString = "a:" + a // method
 val map = Map (1 -> "one", 2 -> "two") // map: Map [Int, String] 

tuples (they are tuples):
  def error = ("Not found", 404) // method
 val (msg, code) = error // msg == "Not found", code == 404 

and ending with functional collections:
  List (1, 2, 3, 4, 5) .filter (_% 2 == 1) .map (_ * 2) .mkString (",") // "2,6,10" 

by mixing traits (as interfaces in Java, only with the possibility of implementation):
  val handler = new DefaultHandler with Logging with AdminRoleRequired with TransactionSupport 

extension of classes (including primitive types) through implicit conversions:
  val date = today + 1.month + 5.days 

and, of course, case classes:
  case class Person (name: String, age: Int) // getters, equals, hashCode, toString and more
 val p = Person ("Vasia", 12) 


Qualitative mix of functional and OO approaches


Scala is the first “broad profile” language with static typing and a well-thought-out (and somewhere supported by dissertations) mix of functional and object-oriented approaches:

Not obvious, but such an approach pleasantly surprises in practice with its simplicity and reliability — functional programming teaches us to minimally use the mutable state (not to remember that we now have what it is and how to live with it) and divide the program into many small, well-read and automatically easily tested methods, powerful typing and traits allow you to clearly define the requirements and avoid a lot of small errors (if compiled, it will work).

Scala specification by volume - less than Java specification


As one type from the Scala community recently put it, “unlike C ++, Scala becomes simpler the further you study it.” Actually this is the main purpose of the language, and even concluded in its name - SCAlable Language. In the specification, only basic things are given, but on the basis of them it is possible to build new convenient constructions that will look like part of the language and can be used to build complex industrial systems. At the same time, due to the weakening of the syntax (you can omit many constructions - brackets, return types, etc.), the language is convenient even for simple scripts (you can write a .scala file without classes and execute it as a script).
For illustration - a recent post about BASIC on Scala .

Not only valuable fur


In addition to the “amenities” noted above, there are named parameters and default parameters:
  def box (width: Int = 100, height: Int = 200) = ... // method
 box (height = 300) // same as box (100, 300) 

by-name parameters (calculated only when necessary):
  def debug (msg: => String) = if (debugEnabled) println (msg)
 debug ("Debug message:" + heavyMethod ()) // heavyMethod will only be called if debugEnabled 

"Lazy" initialization (optimized and thread safe):
  class Context {lazy val (user, password) = {/ * heavy initialization * /}} 

XML support right in the source code:
  def description = <div> <h4> {label} </ h4> {text} </ div> 

advanced access level system:
  protected [package] val a // visible in the package and to the heirs
 private [this] val b // only visible to this instance of this class. 

and, of course, super powerful pattern matching:
  case class Address (city: String, street: String)
 case class Person (name: String, age: Int, address: Address)
 somePerson match {
   case Person ("Vasia", _, _) => ... // name == "Vasia", the rest is not important
   case Person (_, n, _) if n <18 => ... // age <18
   case Person (_, _, Address (_, "Apricot")) => ... // only from Abrikosova street
   case _ => ... // everything else
 } 


And much more


It is very difficult to collect all the interesting things in one post. It is too much. Just do not even remember at a time. What else would you like to add:


About myself: I have been programming in Java for about 10 years, last year we managed to convince our bosses to try Scala in our projects, the first project was a self-documenting JSON / REST web service, everyone liked it, now I'm doing a trial version of a new web interface to a great service (~ 3500 java classes) on Scala + Vaadin.

Source: https://habr.com/ru/post/99347/


All Articles