@@ -274,7 +274,7 @@ second.
274
274
``` scala
275
275
object Timer {
276
276
def oncePerSecond (callback : () => Unit ): Unit = {
277
- while (true ) { callback(); Thread sleep 1000 }
277
+ while (true ) { callback(); Thread . sleep( 1000 ) }
278
278
}
279
279
def timeFlies (): Unit = {
280
280
println(" time flies like an arrow..." )
@@ -319,7 +319,7 @@ program, passing an anonymous function to `oncePerSecond` instead of `timeFlies`
319
319
``` scala
320
320
object TimerAnonymous {
321
321
def oncePerSecond (callback : () => Unit ): Unit = {
322
- while (true ) { callback(); Thread sleep 1000 }
322
+ while (true ) { callback(); Thread . sleep( 1000 ) }
323
323
}
324
324
def main (args : Array [String ]): Unit = {
325
325
oncePerSecond(() =>
@@ -802,9 +802,9 @@ several operations on the expression `(x+x)+(7+y)`: it first computes
802
802
its value in the environment ` { x -> 5, y -> 7 } ` , then
803
803
computes its derivative relative to ` x ` and then ` y ` .
804
804
805
- {% tabs hello-world-demo class=tabs-scala-version %}
805
+ {% tabs calc-main class=tabs-scala-version %}
806
806
807
- {% tab 'Scala 2' for=hello-world-demo %}
807
+ {% tab 'Scala 2' for=calc-main %}
808
808
``` scala
809
809
import Tree ._
810
810
@@ -825,7 +825,7 @@ object Calc {
825
825
```
826
826
{% endtab %}
827
827
828
- {% tab 'Scala 3' for=hello-world-demo %}
828
+ {% tab 'Scala 3' for=calc-main %}
829
829
``` scala
830
830
import Tree .*
831
831
@@ -966,7 +966,7 @@ follows the class name and parameters. It declares that the
966
966
Then, we redefine the ` equals ` method, inherited from
967
967
` Object ` , so that it correctly compares dates by comparing their
968
968
individual fields. The default implementation of ` equals ` is not
969
- usable, because as in Java it compares objects physically . We arrive
969
+ usable, because as in Java it compares objects by their identity . We arrive
970
970
at the following definition:
971
971
972
972
{% tabs equals-definition class=tabs-scala-version %}
@@ -976,11 +976,10 @@ at the following definition:
976
976
class Date (y : Int , m : Int , d : Int ) extends Ord {
977
977
// previous decls here
978
978
979
- override def equals (that : Any ): Boolean =
980
- that.isInstanceOf [Date ] && {
981
- val o = that.asInstanceOf [Date ]
982
- o.day == day && o.month == month && o.year == year
983
- }
979
+ override def equals (that : Any ): Boolean = that match {
980
+ case d : Date => d.day == day && d.month == month && d.year == year
981
+ case _ => false
982
+ }
984
983
985
984
// rest of implementation will go here
986
985
}
@@ -992,11 +991,9 @@ class Date(y: Int, m: Int, d: Int) extends Ord {
992
991
class Date (y : Int , m : Int , d : Int ) extends Ord :
993
992
// previous decls here
994
993
995
- override def equals (that : Any ): Boolean =
996
- that.isInstanceOf [Date ] && {
997
- val o = that.asInstanceOf [Date ]
998
- o.day == day && o.month == month && o.year == year
999
- }
994
+ override def equals (that : Any ): Boolean = that match
995
+ case d : Date => d.day == day && d.month == month && d.year == year
996
+ case _ => false
1000
997
1001
998
// rest of implementation will go here
1002
999
end Date
@@ -1005,17 +1002,12 @@ end Date
1005
1002
1006
1003
{% endtabs %}
1007
1004
1008
- This method makes use of the predefined methods ` isInstanceOf `
1009
- and ` asInstanceOf ` . The first one, ` isInstanceOf ` ,
1010
- corresponds to Java's ` instanceof ` operator, and returns true
1011
- if and only if the object on which it is applied is an instance of the
1012
- given type. The second one, ` asInstanceOf ` , corresponds to
1013
- Java's cast operator: if the object is an instance of the given type,
1014
- it is viewed as such, otherwise a ` ClassCastException ` is
1015
- thrown.
1016
-
1017
- Finally, the last method to define is the predicate which tests for
1018
- inferiority, as follows. It makes use of another method,
1005
+ While in Java (pre 16) you might use the ` instanceof ` operator followed by a cast
1006
+ (equivalent to calling ` that.isInstanceOf[Date] ` and ` that.asInstanceOf[Date] ` in Scala);
1007
+ in Scala it is more idiomatic to use a _ type pattern_ , shown in the example above which checks if ` that ` is an
1008
+ instance of ` Date ` , and binds it to a new variable ` d ` , which is then used in the right hand side of the ` case ` .
1009
+
1010
+ Finally, the last method to define is the ` < ` test, as follows. It makes use of another method,
1019
1011
` error ` from the package object ` scala.sys ` , which throws an exception with the given error message.
1020
1012
1021
1013
{% tabs lt-definition class=tabs-scala-version %}
@@ -1025,14 +1017,13 @@ inferiority, as follows. It makes use of another method,
1025
1017
class Date (y : Int , m : Int , d : Int ) extends Ord {
1026
1018
// previous decls here
1027
1019
1028
- def < (that : Any ): Boolean = {
1029
- if (! that.isInstanceOf [Date ])
1030
- sys.error(" cannot compare " + that + " and a Date" )
1020
+ def < (that : Any ): Boolean = that match {
1021
+ case d : Date =>
1022
+ (year < d.year) ||
1023
+ (year == d.year && (month < d.month ||
1024
+ (month == d.month && day < d.day)))
1031
1025
1032
- val o = that.asInstanceOf [Date ]
1033
- (year < o.year) ||
1034
- (year == o.year && (month < o.month ||
1035
- (month == o.month && day < o.day)))
1026
+ case _ => sys.error(" cannot compare " + that + " and a Date" )
1036
1027
}
1037
1028
}
1038
1029
```
@@ -1043,14 +1034,13 @@ class Date(y: Int, m: Int, d: Int) extends Ord {
1043
1034
class Date (y : Int , m : Int , d : Int ) extends Ord :
1044
1035
// previous decls here
1045
1036
1046
- def < (that : Any ): Boolean =
1047
- if ! that.isInstanceOf [Date ] then
1048
- sys.error(" cannot compare " + that + " and a Date" )
1037
+ def < (that : Any ): Boolean = that match
1038
+ case d : Date =>
1039
+ (year < d.year) ||
1040
+ (year == d.year && (month < d.month ||
1041
+ (month == d.month && day < d.day)))
1049
1042
1050
- val o = that.asInstanceOf [Date ]
1051
- (year < o.year) ||
1052
- (year == o.year && (month < o.month ||
1053
- (month == o.month && day < o.day)))
1043
+ case _ => sys.error(" cannot compare " + that + " and a Date" )
1054
1044
end <
1055
1045
end Date
1056
1046
```
0 commit comments