Understanding Scala's Class Hierarchy and Value Classes

Slide Note
Embed
Share

Scala's class hierarchy includes Any, AnyVal, and AnyRef classes, with implicit conversions and implementations of primitives similar to Java. Learn about defining value classes, natural and reference equality comparisons, and more in Scala programming.


Uploaded on Sep 28, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Chapter 11 Scala s Hierarchy

  2. Chapter 11 Class Hierarchy Primitive Implementation Bottom Types Define Your Own Value Classes

  3. Scalas Class Hierarchy Class Any - Root of the class hierarchy final def ==(that: Any): Boolean final def !=(that: Any): Boolean - Some of its methods are def equals(that: Any): Boolean def ##: Int def hashCode: Int def toString: String

  4. AnyVal - Subclass of Any - Parent class of value classes - Byte - Long - Unit - Short - Float - Double - Char - Int - Boolean

  5. Implicit Conversions - Exist between value class types - Applied whenever it s necessary

  6. Example 42 max 43 42 min 43 1 until 5 1 to 5 3.abs (-3).abs Int RichInt

  7. AnyRef - Root class of all reference classes - Same as Object class in Java - Can use Object and AnyRef - Don t do it

  8. How Primitives Are Implemented - Similar to Java - Except there is one difference - Boxing

  9. Java boolean isEqual(int x, int y) { return x == y; } System.out.println(isEqual(421, 421)); boolean isEqual(Integer x, Integer y) { return x == y; } System.out.println(isEqual(421, 421));

  10. Scala def isEqual(x: Int, y: Int) = x == y isEqual(421, 421) def isEqual(x: Any, y: Any) = x == y isEqual(421, 421)

  11. Java String x = twin ; String y = twin ; System.out.println(x == y); - Compares reference equality

  12. Scala val x = twin val y = twin println(x == y) - Compares natural equality

  13. eq and ne - Behave like Java s == and != val x = twin val y = twin println(x eq y) //false println(x ne y) //true

  14. Bottom Types - Two classes at the bottom of Scala s hierarchy: - Null - Nothing

  15. Null Class - Type of the null reference - Not friendly with value types I don t like you Null Int

  16. Nothing Class - There is no values of type Nothing Why does Nothing exist? - It can be used to signal a wacky termination

  17. Example def error(message: String): Nothing = throw new RuntimeException(message) def cutestAnimal(animal: String): String = if(animal == Cat ) println( You re right ) else sys.error( You re wrong )

  18. Defining Your Own Value Classes - Your class will need: - Exactly one parameter - Must be empty except for defs - Should not redefine equals or hashCode

  19. Value class class Cats(val number: Int) extends AnyVal { override def toString() = amount + cats } val cats = new Cats(20) println(cats) //prints 20 cats

  20. Avoiding a types monoculture - Let the compiler help you - Define a new class for every domain concept - Things can get messy if you don t

  21. HTML Example def title(text: String, anchor: String, style: String): String = s"<a id='$anchor'><h1 class='$style'>$text</h1></a>" title("chap:vcls", "bold", "Value Classes") - Let the compiler help you

  22. class Anchor(val value: String) extends AnyVal class Style(val value: String) extends AnyVal class Text(val value: String) extends AnyVal class Html(val value: String) extends AnyVal def title(text: Text, anchor: Anchor, style: Style): Html = new Html( s"<a id='${anchor.value}'>" + s"<h1 class='${style.value}'>" + text.value + "</h1></a>" )

  23. title(new Anchor("chap:vcls"), new Style("bold"), new Text("Value Classes")) error: type mismatch; found : Anchor required: Text error: type mismatch; found : Style required: Anchor new Text("Value Classes")) On line 2: error: type mismatch; found : Text required: Style

Related


More Related Content