Skip to content

Commit 658419d

Browse files
committed
address comments
1 parent 6dda4d2 commit 658419d

File tree

1 file changed

+31
-41
lines changed

1 file changed

+31
-41
lines changed

_overviews/tutorials/scala-for-java-programmers.md

+31-41
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ second.
274274
```scala
275275
object Timer {
276276
def oncePerSecond(callback: () => Unit): Unit = {
277-
while (true) { callback(); Thread sleep 1000 }
277+
while (true) { callback(); Thread.sleep(1000) }
278278
}
279279
def timeFlies(): Unit = {
280280
println("time flies like an arrow...")
@@ -319,7 +319,7 @@ program, passing an anonymous function to `oncePerSecond` instead of `timeFlies`
319319
```scala
320320
object TimerAnonymous {
321321
def oncePerSecond(callback: () => Unit): Unit = {
322-
while (true) { callback(); Thread sleep 1000 }
322+
while (true) { callback(); Thread.sleep(1000) }
323323
}
324324
def main(args: Array[String]): Unit = {
325325
oncePerSecond(() =>
@@ -802,9 +802,9 @@ several operations on the expression `(x+x)+(7+y)`: it first computes
802802
its value in the environment `{ x -> 5, y -> 7 }`, then
803803
computes its derivative relative to `x` and then `y`.
804804

805-
{% tabs hello-world-demo class=tabs-scala-version %}
805+
{% tabs calc-main class=tabs-scala-version %}
806806

807-
{% tab 'Scala 2' for=hello-world-demo %}
807+
{% tab 'Scala 2' for=calc-main %}
808808
```scala
809809
import Tree._
810810

@@ -825,7 +825,7 @@ object Calc {
825825
```
826826
{% endtab %}
827827

828-
{% tab 'Scala 3' for=hello-world-demo %}
828+
{% tab 'Scala 3' for=calc-main %}
829829
```scala
830830
import Tree.*
831831

@@ -966,7 +966,7 @@ follows the class name and parameters. It declares that the
966966
Then, we redefine the `equals` method, inherited from
967967
`Object`, so that it correctly compares dates by comparing their
968968
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
970970
at the following definition:
971971

972972
{% tabs equals-definition class=tabs-scala-version %}
@@ -976,11 +976,10 @@ at the following definition:
976976
class Date(y: Int, m: Int, d: Int) extends Ord {
977977
// previous decls here
978978

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+
}
984983

985984
// rest of implementation will go here
986985
}
@@ -992,11 +991,9 @@ class Date(y: Int, m: Int, d: Int) extends Ord {
992991
class Date(y: Int, m: Int, d: Int) extends Ord:
993992
// previous decls here
994993

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
1000997

1001998
// rest of implementation will go here
1002999
end Date
@@ -1005,17 +1002,12 @@ end Date
10051002

10061003
{% endtabs %}
10071004

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,
10191011
`error` from the package object `scala.sys`, which throws an exception with the given error message.
10201012

10211013
{% tabs lt-definition class=tabs-scala-version %}
@@ -1025,14 +1017,13 @@ inferiority, as follows. It makes use of another method,
10251017
class Date(y: Int, m: Int, d: Int) extends Ord {
10261018
// previous decls here
10271019

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)))
10311025

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")
10361027
}
10371028
}
10381029
```
@@ -1043,14 +1034,13 @@ class Date(y: Int, m: Int, d: Int) extends Ord {
10431034
class Date(y: Int, m: Int, d: Int) extends Ord:
10441035
// previous decls here
10451036

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)))
10491042

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")
10541044
end <
10551045
end Date
10561046
```

0 commit comments

Comments
 (0)