こんにちは、Habr。「Scala-developer」コースの将来の学生のために、資料の翻訳を用意しました。
また、オープンウェビナー「 EffectsinScala 」にもご招待します。参加者は、専門家とともに、効果の概念と、それらが存在する場合に発生する可能性のある複雑さを検討し、機能的効果の概念とその特性を検討します。
Scala 3とは何ですか?
Scala 3は、Scalaプログラミング言語の新しいメジャーバージョンです。これは、他の多くの人々や組織の助けを借りてScalaの開発を調整し、それを可能にするために自由な時間を投資する企業や組織間の長年の研究、開発、およびコラボレーションの結果です。これらの共同作業により、言語に最も顕著な変化がもたらされました。
, 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 — , :
, (, /, );
( , 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
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 , , , .