About Scala for those who need little Java, and more

    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 harsh people, accustomed to serious standards, and with high demands. Therefore, most new languages ​​hang on the "periphery."

    Scala differs from other languages ​​in the JVM by a truly thorough approach - the laboratory of the most powerful European EPFL Institute headed by Professor Martin Odersky, who is also known as a designer of generic systems from Java 1.5, works on the language . Of course, this can’t be compared in terms of support to serious commercial offices like Sun or Microsoft, so the language developed slowly and “went into business” quite recently:
    scala Job Trends graph

    What is interesting about it?

    First of all, the familiar syntax and accessibility of the baggage of Java developments and free mixing with it - you can safely add several Scala classes to an existing Java project and everything will work (Scala has extensions that are not available from Java, but it’s usually not difficult to decide the opposite everything is 100% accessible).

    Concise and expressive code

    Starting with the simplest types of inference and syntax relaxations:
    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"

    mixing traits (like 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 “wide profile” language with static typing and a well-thought-out (and somewhere backed by dissertations) mixture of functional and object-oriented approaches:

    It is not obvious, but this approach pleasantly surprises in practice with its simplicity and reliability - functional programming teaches you to use mutable state minimally (not to keep in mind that we have the same thing 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 set requirements and avoid a bunch of minor errors (if compiled, it will work).

    Scala Volume Specification - Less Java Specification

    As one type from the Scala community recently put it, “Unlike C ++, Scala becomes simpler the further you learn it.” Actually this is the main purpose of the language, and even lies in its name - SCAlable LАnguage. Only basic things are given in the specification, but on the basis of them you can 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 syntax weaknesses (you can omit many constructions - brackets, return types, etc.), the language turns out to be convenient even for simple scripts (you can write a .scala file without classes and execute it as a script).
    To illustrate, 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 if necessary):
    def debug (msg: => String) = if (debugEnabled) println (msg)
    debug ("Debug message:" + heavyMethod ()) // heavyMethod is only 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 = 



    advanced access level system:
    protected [package] val a // visible in the package and to the heirs
    private [this] val b // is visible only 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 Apricot Street
      case _ => ... // everything else

    And much more

    It is very difficult to collect everything interesting in one post. Its too much. Just do not even remember at a time. What else would you like to add:
    • Scala is used, for example, in: Twitter, LinkedIn, FourSquare (1.5 million users, 31 million views per month, 20 million API requests per day), Sony, Siemens
    • The first version of Lift , the popular web framework for Scala, was released a little over a year ago, and is already used in such serious projects as Foursquare and Novell Pulse
    • Akka library shows very good results in transferring Erlang achievements in the field of distributed systems to the JVM infrastructure
    • For those who understand conversational English, I recommend Odersky's introduction video at the recent Scala Days conference. There were many other interesting reports .
    • I recommend IntelliJ IDEA Community Edition + Scala plugin as an IDE, the Eclipse plugin already looks good too, you can try
    • To track interesting links on a topic, you can, for example, read (not mine): twitter.com/implicit_ly , twitter.com/ScalaAtSO , twitter.com/bubbl_scala

    About me: I’ve been programming in Java for about 10 years, last year I managed to convince the authorities 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 the new web interface for a great service (~ 3500 java classes) on Scala + Vaadin.

    Also popular now: