A la veille du cours "Scala developer", nous vous invitons au webinaire ouvert "Functional Programming in Scala" .
Pour l'instant, partageons une traduction traditionnelle et utile.
. , Java. ( ) Java 15, — Java 17 LTS ( ).
Java , JVM . , , ( var) switch-. Scala — , - .
, Java 15 ( ) Scala. , JVM . , , Java ( -).
:
,
private final
,
-
equals
,hashCode
toString
class Person {
private final String name;
private final int age;
Person(String name, int age) {
this.name = name;
this.age = get;
}
String name() { return name; }
int age() { return age; }
public boolean equals(Object o) {
if (!(o instanceof Person)) return false;
Person other = (Person) o;
return other.name == name && other.age = age;
}
public int hashCode() {
return Objects.hash(name, age);
}
public String toString() {
return String.format("Person[name=%s, age=%d]", name, age);
}
}
, , IDE (. Lombok). , . !
:
record Person(String name, int age) { }
21 ! . , :
var john = new Person("john", 76);
Scala — case- ( ). :
case class Person(name: String, age: Int)
case-?
equals
,hashCode
toString
( ).:
private final
+ - Java,val
Scala..
/case- .
interface
, case- —trait
( Java-interface
Scala).java.lang.Record
, case-scala.Product
.
:
: private public. , ; , , . Scala case- private public, .
, . Scala case- , ( : case- case-).
(), , case- ( ).
, . , :
record Person(String name, int age) {
Person {
if (age < 0)
throw new IllegalArgumentException("Too young");
}
}
Scala .
9 . (Loïc Descotte) (Jarek Ratajski), case- Scala: copy.
( - ), . Copy — Scala, , !
, Scala case
class
: , , case-; . Java — «», , ( enum
).
, Animal
:
public sealed interface Animal permits Cat, Dog, Elephant {...}
:
public class Cat implements Animal { ... }
public class Dog implements Animal { ... }
public class Elephant implements Animal { ... }
permits
, , . , , , Java .
Scala (sealed
), . permits
. , , . , Java, Scala, , , ( ):
sealed trait Animal
class Cat extends Animal { ... }
class Dog extends Animal { ... }
class Elephant extends Animal { ... }
(, Scala public
, , Java package-private).
Java Scala case-. , . case- product, sum.
? Java :
final
, ;sealed
,permits;
non-sealed
, .
, . :
public sealed interface Animal permits Cat, Dog, Elephant
public final class Cat implements Animal { ... }
public sealed class Dog permits Chihuahua, Pug implements Animal {}
public non-sealed class Elephant implements Animal { ... }
, non-sealed
, , , .
Scala ; :
final
sealed
( );, .
( ) non-sealed
:
sealed trait Animal
final class Cat extends Animal { ... }
sealed class Dog extends Animal { ... }
class Elephant extends Animal { ... }
instanceof
, , , , « », instanceof:
if (myValue instanceof String s) {
// s is in scope and is a String
} else {
// s is not in scope
}
var s = (String) myValue
if
, , if (, ).
- flow-typing TypeScript, ( s
) .
myValue match {
case s: String => // s is in scope and is a String
case _ => // s is not in scope
}
, .
Java , , :
if (myValue instanceof String s && s.length() > 42) { ... }
Scala :
myValue match {
case s: String if s.length() > 42 => ...
}
, Java , — :
var isLongString = myValue instanceof String s && s.length() > 42
Scala, , ( ), :
val isLongString = myValue match {
case s: String if s.length() > 42 => true
case _ => false
}
, Scala — . , case-, .
Java? , . sealed , . Scala match
- sealed
.
"""
, :
var response = """
<html>
<body>Internal server error</body>
</html>"""
"""
, . . :
<html>
<body>Internal server error</body>
</html>
, — , ! : . , .
Scala , , :
val response = """<html>
<body>Internal server error</body>
</html>"""
Scala — . :
val response = """<html>
| <body>Internal server error</body>
|</html>""".stripMargin)
( |
, stripMargin
.)
, Scala, , Java .
Scala . Java , String::formatted
. Scala s
:
val message = "Internal server error"
val response = s"""<html>
| <body>$message</body>
|</html>""".stripMargin)
Scala, Java — . «» . Scala Java . , Java.
, Java, — , Java-. , «» .
, , . , switch-, Java 12, switch
(, , ). Scala , .
— , , . — Scala class
, Java .
, Scala , Java, — , . , Scala , , , , ( ) .
Java 16 ( ), Scala , ! , Scala Scala Times.