Scala 3 / Dotty - Fakten und Meinungen. Was erwarten wir?

Hallo Habr. Für zukünftige Studenten des Kurses "Scala-Entwickler" haben wir eine Übersetzung des Materials vorbereitet.



Wir laden Sie auch zum offenen Webinar "Effekte in Scala" ein . Die Teilnehmer werden zusammen mit einem Experten das Konzept der Wirkung und die Komplexität, die auftreten kann, wenn sie existieren, sowie das Konzept einer funktionalen Wirkung und ihre Eigenschaften berücksichtigen.






Was ist Scala 3?

Scala 3 ist die neue Hauptversion der Programmiersprache Scala. Es ist das Ergebnis jahrelanger Forschung, Entwicklung und Zusammenarbeit zwischen Unternehmen und Organisationen, die die Entwicklung von Scala mit Hilfe vieler anderer Personen und Organisationen koordinieren und ihre Freizeit investieren, um dies zu ermöglichen. Diese Zusammenarbeit hat uns die bemerkenswertesten Veränderungen in der Sprache gebracht. 





, 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