diff --git a/src/main/scala/scalatutorial/sections/ClassesVsCaseClasses.scala b/src/main/scala/scalatutorial/sections/ClassesVsCaseClasses.scala index 81fb9f7b..7a91afb0 100644 --- a/src/main/scala/scalatutorial/sections/ClassesVsCaseClasses.scala +++ b/src/main/scala/scalatutorial/sections/ClassesVsCaseClasses.scala @@ -155,7 +155,7 @@ object ClassesVsCaseClasses extends ScalaTutorialSection { * } * * // toString redefinition to return the value of an instance instead of its memory addres - * override def toString = s"Note($name, $duration, $octave)" + * override def toString = s"Note($name,$duration,$octave)" * * // Create a copy of a case class, with potentially modified field values * def copy(name: String = name, duration: String = duration, octave: Int = octave): Note = @@ -170,12 +170,19 @@ object ClassesVsCaseClasses extends ScalaTutorialSection { * new Note(name, duration, octave) * * // Extractor for pattern matching - * def unapply(note: Note): Option[(String, String, Int)) = + * def unapply(note: Note): Option[(String, String, Int)] = * if (note eq null) None * else Some((note.name, note.duration, note.octave)) * * } * }}} */ - def nothing(): Unit = () + def encoding(res0: String, res1: Boolean, res2: String): Unit = { + val c3 = Note("C", "Quarter", 3) + c3.toString shouldBe res0 + val d = Note("D", "Quarter", 3) + c3.equals(d) shouldBe res1 + val c4 = c3.copy(octave = 4) + c4.toString shouldBe res2 + } } diff --git a/src/main/scala/scalatutorial/sections/LazyEvaluation.scala b/src/main/scala/scalatutorial/sections/LazyEvaluation.scala index f18c6b55..b24398a5 100644 --- a/src/main/scala/scalatutorial/sections/LazyEvaluation.scala +++ b/src/main/scala/scalatutorial/sections/LazyEvaluation.scala @@ -209,6 +209,18 @@ object LazyEvaluation extends ScalaTutorialSection { * lazy val x = expr * }}} * + * = Lazy Vals and Streams = + * + * Using a lazy value for `tail`, `Stream.cons` can be implemented more efficiently: + * + * {{{ + * def cons[T](hd: T, tl: => Stream[T]) = new Stream[T] { + * def head = hd + * lazy val tail = tl + * … + * } + * }}} + * * == Exercise == */ def lazyVal(res0: String): Unit = { @@ -223,18 +235,4 @@ object LazyEvaluation extends ScalaTutorialSection { builder.result() shouldBe res0 } - /** - * = Lazy Vals and Streams = - * - * Using a lazy value for `tail`, `Stream.cons` can be implemented more efficiently: - * - * {{{ - * def cons[T](hd: T, tl: => Stream[T]) = new Stream[T] { - * def head = hd - * lazy val tail = tl - * … - * } - * }}} - */ - def nothing(): Unit = () } diff --git a/src/main/scala/scalatutorial/sections/StructuringInformation.scala b/src/main/scala/scalatutorial/sections/StructuringInformation.scala index 594bce65..7a1bcb87 100644 --- a/src/main/scala/scalatutorial/sections/StructuringInformation.scala +++ b/src/main/scala/scalatutorial/sections/StructuringInformation.scala @@ -141,20 +141,20 @@ object StructuringInformation extends ScalaTutorialSection { * the possible case of symbols is fixed. The compiler can leverage this * knowledge to warn us if we write code that does not handle ''all'' * the cases: - */ - def unexhaustive(): Unit = { - sealed trait Symbol - case class Note(name: String, duration: String, octave: Int) extends Symbol - case class Rest(duration: String) extends Symbol - - def nonExhaustiveDuration(symbol: Symbol): String = - symbol match { - case Rest(duration) => duration - } - } - - /** - * Try to run the above code to see how the compiler informs us that + * {{{ + * def unexhaustive(): Unit = { + * sealed trait Symbol + * case class Note(name: String, duration: String, octave: Int) extends Symbol + * case class Rest(duration: String) extends Symbol + * + * def nonExhaustiveDuration(symbol: Symbol): String = + * symbol match { + * case Rest(duration) => duration + * } + * } + *}}} + * + * If we try to run the above code to see how the compiler informs us that * we don’t handle all the cases in `nonExhaustiveDuration`. * * = Equals = diff --git a/src/test/scala/scalatutorial/sections/ClassesVsCaseClassesSpec.scala b/src/test/scala/scalatutorial/sections/ClassesVsCaseClassesSpec.scala index 101d5105..46d4ed29 100644 --- a/src/test/scala/scalatutorial/sections/ClassesVsCaseClassesSpec.scala +++ b/src/test/scala/scalatutorial/sections/ClassesVsCaseClassesSpec.scala @@ -19,4 +19,10 @@ class ClassesVsCaseClassesSpec extends Spec with Checkers { def `check equality`: Unit = check(Test.testSuccess(ClassesVsCaseClasses.equality _, false :: true :: HNil)) + def `check encoding`: Unit = + check( + Test.testSuccess( + ClassesVsCaseClasses.encoding _, + "Note(C,Quarter,3)" :: false :: "Note(C,Quarter,4)" :: HNil)) + }