A wonderful present and bright future for Scala

    image

    More recently, an article appeared on Habré that gives an incorrect idea of ​​the current state of affairs in the Scala community. The reader creates a false impression of stagnation and decay in the Scala ecosystem. In this article, I would like to fix this and talk about recently released new products and future changes.

    In May 2016, a presentation was presented at the Scala Days conference in New York by Martin Odersky describing the present and future of the Scala language.

    Scala 2.12


    In November 2016, a new version of the Scala compiler version 2.12 was released, in which traits are compiled directly into Java interfaces with default methods, and the lambda functions in Scala and Java have become fully compatible, so now they can be called in both directions equally conveniently. It also contributed to improving the performance of Scala code on JRE 8.

    Scala 2.13


    The new release of the scalac compiler is scheduled to refactor the standard library. Particular attention will be paid to collections and perhaps the standard library will be divided into a compact kernel and a platform that will include everything that does not fall into the kernel. It seems as though the development of scalac is stalling in place. Perhaps this is partly true, because the pulp itself is being developed for the new Scala compiler, baptized by Dotty in honor of the dependent type theory on which it is based.

    Scala dotty


    The development of the new compiler has been ongoing for several years by the EPFL team, led by Martin Odersky. In addition to him, another 5 people are included in the core of the team, including Khabrovchan Dmitry Petrashko aka darkdimius . The main for the new version of the language is Dependent Object Types calculus, developed by Martin Odersky specifically for the needs of Scala.

    Due to the new compiler structure, the essence of which is explained in this video , the compilation speed is already twice as fast as in scalac. And this is taking into account the fact that the developers have not even started to optimize the compiler. So all myths that are already outdated at the moment about the inconvenience of the Scala brake compilation will be completely debunked.

    In Dotty the type system will be updated, unused features of the language will be removed. Many things will be greatly simplified. But these are all abstract words. Let's move on to the examples quickly.

    First, let's go through what was removed as unnecessary.

    • Procedural Syntax
    • Classes with DelayedInit
    • Reflection-based macros
    • Early Trait Initialization
    • Existential types
    • Projections of generic types
    • XML embed code

    Instead, a total of new and improved features have been added:

    • Now instead of with in the variable type, intersection types are added

      val a : A & B

    • Now, instead of problems with Either, we have union types. A variable can store either type A or type B. This eliminates a bunch of boilerplate

      val a : A | B

    • Tuples in lambda expressions finally fixed.

      
      //Было
      val numbersAndLetters = List((1,"A"), (2,"B"))
      numbersAndLetters.map {
         case (number, letter) => ...
      }
      //Стало
      numbersAndLetters.map((number, letter) => ...)
      

    • In traits you can declare parameters

      
      trait WithParameter(name:String) {
      ...
      }
      

    • Equality operators == and inequalities! = Are now type-safe
    • Type parameters can now be assigned names. Partial parameterization has become possible.

      
      trait Map[type Key, type Value]
      Map[Key = Int]
      

    In its current form, the dotc compiler can be tested on this test project . In the future, it is promised to add new features to the language.

    • Implicit Functional Type

      
      type CtxS = implicit Context => S
      def f(x:T):CtxS = {
       ... implicitly[Context] ...
      }
      f(e) //Context argument is passed implicitly
      

    • Along with the usual functions, effects guaranteeing purity will appear

      
      A => B (can be impure)
      A -> B (pure)
      

    • Regular types cannot be null by default. Values ​​that come from Java code will always be optional.

      
      T? = T | Null
      val stream: PrintStream? = System.out.printStream
      

    • The magic number 22 will no longer scare Scala developers. Tuple22 / Function22 / Product22 will become history

      
      type (S, T, U) = (S, (T, (U, Unit)))
      

    • New records data type. Something like tyupla with names. There is no example code. This is by far the most mystical concept presented.

    Also, Dasty has designed the Tasty system, which allows not to recompile libraries for each version of the compiler, which was problematic in Scala 2. And compared to the second version of Scala, Dotty promises to make good explanations for compilation errors.

    Dotty linker


    The crown on the Dotty crown is Linker , developed by Dmitry Petrashko. This is an optimizer that allows you to compile code exclusively for those types that are actually used. This allows you to achieve a significant reduction in the amount of code and increase productivity. With the help of the linker, even the performance of the connected libraries is greatly improved. You can learn more about this miracle optimizer in this video .

    Scala meta


    Instead of old macros, new convenient Scala Meta macros are being developed, developed by Eugene Burmako. So far, they have been released only for Scala 2.11 and only for annotations, but not methods. But we can already say that they turned out much more convenient than the previous version. Now the macro declaration and its implementation are not separated, and the work with syntax trees is simply magnificent. Scala.meta also works with source trees, and not with already slightly transformed ones like macros based on the old scala.reflect.
    You can read more about them in this tutorial .

    Scala js


    Scala.js compiles Scala code into high-performance Javascript code. Together with Scalatags and ScalaCSS , a full-fledged web frontend development is possible. A huge plus is the ability to share entities between the client and the server, and use the remote procedure call instead of the explicit HTTP API definition. In the last few years, there have been several well-established and used in production production frameworks with reactive binders like Udash . Many developers use Scala.js because they don’t want to suffer with dynamic and “strange” JS. I will give an example used in the official documentation.

    javascript> ["10", "10", "10", "10"].map(parseInt)
    [10, NaN, 2, 3] // What???
    scala> List("10", "10", "10", "10").map(parseInt)
    List(10, 10, 10, 10) // That's it 
    

    Scala native


    Scala Native , a native binary compiler based on LLVM, has recently been released . This allows you to write instantly running code on Scala and use low-level data structures.

    Macroid and RoboScala


    Since Scala is compiled into Java code, it is possible to use this language for development for Android. To do this, there is a specialized Macroid library that provides an easy-to-use Android API. It is also possible to compile Scala code on iOS using RoboScala - wrappers around RoboVM

    Scalafx


    To develop desktop software, there is a ScalaFX library , which is a wrapper around the famous JavaFX.

    Conclusion


    Although the article turned out to be very messy, it can serve as a good list of links to various Scala compilation tools for various platforms, and a source of information about future developments and features. All the tools and compilers presented in the article are actively developed, improved and used. So there is no need to talk about any stagnation or even decline in the Scala ecosystem.

    Also popular now: