Scala 3 / Dotty - Facts and Opinions. What are we expecting?

Hello, Habr. For future students of the "Scala-developer" course, we prepared a translation of the material.



We also invite you to the open webinar "Effects in Scala" . Participants, together with an expert, will consider the concept of effect and the complexity that may arise if they exist, as well as consider the concept of a functional effect and its properties.






What is Scala 3?

Scala 3 is the new major version of the Scala programming language. It is the result of years of research, development and collaboration between companies and organizations that coordinate the development of Scala with the help of many other people and organizations, and who invest their free time to make it possible. These collaborative efforts have brought us the most notable changes in the language. 





, Scala ( DOT- β€” , Scala 3 Dotty); , , ; ; .





, , , Scala. . , .





Scala 3 β€” ?

, , Scala , , . , . , . 





, . , , , ( ), Scala 3 Scala. , - , . 





?

, . Scala 3. . , , . , , . , . , , , , .. , . 





Scala 3 β€” Python 3?

, Scala 3 β€” Python 3 . , : , Scala 3, Scala 2.13 ( ); Scala; 2 3 Scala, 2 3 Python 3.





?

, Scala. , . , . , . , , dotty.epfl.ch.





Optional Braces ( )

optional braces , Python. , β€” , , . , Β« Β». Optional braces β€” , : 





  1. , (, /, );





  2. ( , scalafmt), , .)





trait Printer:
  def print(msg: String): Unit

class ConsolePrinter extends Printer:
  def print(msg: String): Unit = println(msg)

class EmojiPrinter(underlying: Printer) extends Printer:
  def print(msg: String): Unit =
    val emoji = msg match
      case ":)"  => ?
      case ":D"  => ?
      case ":|"  => ?
      case other => other
    underlying.print(emoji)
      
      



, . Scala 3 end . 





class EmojiPrinter(underlying: Printer) extends Printer:
  def print(msg: String): Unit =
    if msg != null then
      val emoji = msg match
        case ":)"  => ?
        case ":D"  => ?
        case ":|"  => ?
        case other => other
      underlying.print(emoji)
    end if
end EmojiPrinter
      
      



, , then. 





β€” β€” β€” , .





, end , . , , .





end . β€” , . , Β« Β» . , , end :





  • ,





  • 15





  • 4





, , .. . , . , , .





Enums

Scala, Java, enum



, . , Scala 3 - , - :





sealed trait Color
case object Red extends Color
case object Green extends Color
case object Blue extends Color
      
      



Scala 3, enum



:





enum Color:
  case Red, Blue, Green
      
      



. , (ADT), . . , Scala 3 ADT enums



:





enum Option[+T]:
  case Some(x: T) // extends Option[T]       (omitted)
  case None       // extends Option[Nothing] (omitted)
      
      



Scala-enum Java-enum, java.lang.Enum, :





enum Color extends Enum[Color]:
  case Red, Blue, Green

println(Color.Green.compareTo(Color.Red)) // 2
      
      



, , ADT, enums.





implicit ()

, implicit Scala. , . , implicit , , . , , implicit , , . Scala 3 implicit, . , implicit .





Implicit β†’





, Scala 3 . implicit . Scala 3 . , .





trait Ord[T]:
  def compare(a: T, b: T): Int

given intOrd: Ord[Int] with // with name
  def compare(a: Int, b: Int): Int = a - b

given Order[String] with // without name
  def compare(a: String, b: String): Int = a.compareTo(b)
      
      



Implicit β†’ clauses





( implicit ) . Scala 3 implicit using



. , min



, .





def min[T](a: T, b: T)(using ord: Ord[T]): T =
  if ord.compare(a, b) < 0 then a else b

min(4, 2)min(1, 2)(using intOrd)
min("Foo", "Bar")
      
      



, .





def printMin[T](a: T, b: T)(using Ord[T]): Unit =
  println(min(a, b))
      
      



Implicit β†’





, implicit , , IDE , implicit . Scala 3 .





object A:
  class TC
  given tc: TC = ???
  def f(using TC) = ???

object B:
  import A._
  import A.given
  ...
      
      



, wildcad (_)



, Scala 3 , . .





object C:
  import A.{using, _}
      
      



, .





Implicit Conversion β†’ Conversion





, Scala 3, Implicit Conversion



, Implicit Conversion



, .  scala.Conversion



, . ,  scala. Conversion



β€” . .





abstract class Conversion[-T, +U] extends (T => U):
  def apply (x: T): U
      
      



, Int Double :





given int2double: Conversion[Int, Double] with
def apply(a: Int): Double = a.toDouble

given Conversion[Int, Double] = _.toDouble
      
      



Conversion



- . , implicit .





Implicit β†’





, Implicit



.





case class Image(width: Int, height: Int, data: Array[Byte])

extension (img: Image)
  def isSquare: Boolean = img.width == img.height

val image = Image(256, 256, readBytes("image.png"))

println(image.isSquare) // true
      
      



, . .





extension [T](list: List[T])
def second: T = list.tail.head
def heads: (T, T) = (list.head, second)
      
      



, «», implicit . , implicit , .





Scala 3 , β€” .





β€” , , . &



. &



: A & B



 B & A



. , .





trait Printable[T]:
 def print(x: T): Unit

trait Cleanable:
 def clean(): Unit

trait Flushable:
 def flush(): Unit

def f(x: Printable[String] & Cleanable & Flushable) =
 x.print("working on...")
 x.flush()
 x.clean()
      
      



, . , . members ( ). , . members , members .





trait A:
  def parent: Option[A]

trait B:
  def parent: Option[B]

class C extends A,B:
  def parent: Option[A & B] = None
  // or
  // def parent: Option[A] & Option[B] = Nil

def work(x: A & B) =
  val parent:[A & B] = x.parent
  // or
  // val parent: Option[A] & Option[B] = x.parent
  println(parent) // None

work(new C)
      
      



, in class C , children member



A B. C β€” A B, , Option[A] & Option[B]



Option[A & B]



, Option



() .





A | B   A B. , , members (), . , members, . 





def parseFloat(value: String | Int): Float = 
  value match 
    case str: String => str.toFloat
    case int: Int => int.floatValue

parseFloat("3.14") // 3.14
parseFloat(42) // 42.0
      
      



. , (val



, var



def



) , , ancestor ().





val any = if (cond) 42 else "3.14" // Any
val union: String | Int = if (cond) 42 else "3.14" // String | Int
      
      



.





 





Scala 3 . . , Scala 3.









Case class



, Case class



, new



. Scala 3 new  . 





Opaque 





Opaque- - . Opaque, , , . Opaque- , . , , Opaque-.





Export clauses





Export clauses β€” members () - . export - ( ) ( ), members .  









Scala 2 . , Scala 2, Scala 3 . Scala 3 , . Scala 3.









, Scala 3 . :





  •  (C#P)   , .. ;





  • , infix



    ;





  • - ;





  • Implicit ;





  • DelayedInit ;





  • ( = );





  • XML , ;





  • , ()



    ;









.





Scala 3?

, , Scala 3. , , Scala 3 .





, , , . Scala. , , . , Scala 3 , .  , , . 





Scala 3?

Scala 3 , , , . , , , Scala 3 , Scala 2.13 ( ), , -.





Scala 3?

Scala 3 Scala 2. , Scala 2. Scala 2.13.4, 2020 , , Scala 3. , Scala 3 . 





Scala 3 Scala. Scala 3 TASTy Pickle Scala 2.x. Scala 2.13.4 TASTy, , , Enums, Intersection types ( ) . .





. , , , .





, , Scala 3 β€” Scala 2. : , , , . , Scala 3 , , , .






Β«Scala-Β».





Β« ScalaΒ».








All Articles