Scala 3.0, an upgrade to the object-oriented, functional Scala language that started out on the JVM, will be a big step towards realizing the full potential of ideas. The intent is to publish the final Scala 3.0 soon after Scala 2.14. At the current release schedule that means early 2020. Let’s discuss 2.12 and 2.13 with the new Dotty compiler in Scala 3.0.
Scala 2.12
Scala 2.12 optimized for Java 8. It uses Java 8’s lambdas and default methods for shorter code and faster execution speed, released with 33 features.
Scala 2.13
Scala 2.13 revamped collections simpler to use with better lazy collections.
Dotty: New Scala Compiler
A bit more than half the size and about twice the speed of current scala compiler, nsc.
- dotty: 45KLoc
- nsc: 75 KLoc
Some of the features that Scala 3.0 offers are listed below:
Trait Parameters
Replaced Early initializers with traits with parameters.
class C extends { trait T(x: Int) {}
val x = e replaced with
} with D
- Traits are not allowed to pass arguments neither to classes nor traits. Classes can pass arguments to parent traits only.
- If class C implements parameterized T but its superClass doesn’t then T is the parent trait of C with arguments. But if superClass implements T trait the, subClass C can’t pass arguments to T.
The order of initialization of traits is unaffected by parameter passing – as always, traits are initialized in linearization order.
Function Arity Adaption
Let say you have a list of pairs
val listOfPairs: List[(Int, Int)]
now you want to map x so that it can sum up. The best solution is to use pattern matching but now Dotty has simplified this as well.
listOfPairs map { listOfPairs map {
case (x, y) => x + y replaced with (x, y) => x + y
} }
@static fields and methods
Some JVM and JavaScript frameworks require classes to have specific static fields and/or methods. To have static methods and fields it needs to be defined in an object
and annotated @static.
object StaticObject {
@static val staticVariable = 5
@static def staticFunction(y: Int): Int = staticVariable + y
}
- @static fields should precede any non-static fields.
- If a member of
foo
anobject C
is annotated with,@static
the companion classC
is not allowed to define term members with the namefoo
. Also, classC
is not allowed to inherit classes that define a term member with namefoo
.
Multiversal Equality
Previously, Scala had universal equality: Two values of any types could be compared with each other with ==
and !=
. This came from the fact that ==
and !=
are implemented in terms of Java’s equals
method, which can also compare values of any two reference types.
Universal equality is convenient but also dangerous since it undermines type safety. Say you have an erroneous program where a value y
has type S
instead of the expected type T
.
val x = ??? // of type T
val y = ??? // of type S, but should be T
x == y // typechecks, will always yield false
Union and Intersection Types
Union types are dual of intersection types. Values of type A | B
are all values of type A
and all values of type B
. |
is commutative: A | B
is the same type as B | A
.
def login(id: UserName | Password) = {
val user = id match {
case UserName(name) => ???
case Password(hash) => ???
}
If a member appears in both A
and B
, its type in A & B
is the intersection of its type in A
and its type in B
. Unlike with
types, &
is commutative: A & B
is the same type as B & A
.
class C extends A with B {
def children: List[A & B] = ???
}
Enumerations
Scala redesigned Enums as well. They can be parameterized and can contain custom members.
object Day extends Enumeration { type Day = Value val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value } //replaced withenum Day { case Mon, Tue, Wed, Thu, Fri, Sat, Sun }
Enums can be parameterized.
enum Day(val mon: Int) {}
Lazy Vals
Lazy vals are not thread safe in dotty. If you need thread safety, you need to mark your lazy vals with @volatile
annotation.
@volatile lazy val x = ???
For more you can refer to the documentation of Dotty consists of New Types, ENUMS, Other New Features, Changed Features and some Dropped Features. You can also refer to the presentation by MARTIN ODERSKY.
I hope you liked the blog. Stay tuned & Happy Blogging!! 🙂