Case classes are like regular classes with a few key differences which we will go over. Case classes are good for modeling immutable data. In the next step of the tour, we’ll see how they are useful in pattern matching.

Defining a case class

A minimal case class requires the keywords case class, an identifier, and a parameter list (which may be empty):

case class Book(isbn: String)

val frankenstein = Book("978-0486282114")

Notice how the keyword new was not used to instantiate the Book case class. This is because case classes have an apply method by default which takes care of object construction.

When you create a case class with parameters, the parameters are public vals.

case class Message(sender: String, recipient: String, body: String)
val message1 = Message("guillaume@quebec.ca", "jorge@catalonia.es", "Ça va ?") println(message1.sender) // prints guillaume@quebec.ca
message1.sender = "travis@washington.us" // this line does not compile

You can’t reassign message1.sender because it is a val (i.e. immutable). It is possible to use vars in case classes but this is discouraged.

Comparison

Case classes are compared by structure and not by reference:

case class Message(sender: String, recipient: String, body: String)

val message2 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?")
val message3 = Message("jorge@catalonia.es", "guillaume@quebec.ca", "Com va?")
val messagesAreTheSame = message2 == message3 // true

Even though message2 and message3 refer to different objects, the value of each object is equal.

Copying

You can create a (shallow) copy of an instance of a case class simply by using the copy method. You can optionally change the constructor arguments.

case class Message(sender: String, recipient: String, body: String)
val message4 = Message("julien@bretagne.fr", "travis@washington.us", "Me zo o komz gant ma amezeg")
val message5 = message4.copy(sender = message4.recipient, recipient = "claire@bourgogne.fr")
message5.sender // travis@washington.us
message5.recipient // claire@bourgogne.fr
message5.body // "Me zo o komz gant ma amezeg"

The recipient of message4 is used as the sender of message5 but the bodyof message4 was copied directly.

最新文章

  1. Asp.net Mvc Entity Framework Code First 数据库迁移
  2. android studio集成融云 SDK 后在部分机型启动对话时崩溃
  3. uC/OS-II互斥信号(OS_mutex)块
  4. 滚动RollUp、压缩
  5. uva 10827
  6. FindBugs
  7. linux 下执行.sh文件总是提示permission denied
  8. 计算机学院大学生程序设计竞赛(2015’12) 1001 The Country List
  9. zoj3432 Find the Lost Sock 亦或的运用
  10. Unity UGUI
  11. 从头到尾彻底解析Hash 表算法
  12. MyBridgeWebViewDemo【集成JsBridge开源库的的封装的webview】
  13. 【Tomcat】Tomcat工作原理
  14. SharePoint 2013 处理videoplayerpage.aspx下的个人图片显示有误问题
  15. linux编码问题小节
  16. 详解ruby的attr_accessor和cattr_accessor
  17. 线段树-hdu2795 Billboard(贴海报)
  18. (2.7)Mysql之SQL基础——表的操作与查看
  19. nodejs学习笔记(2)
  20. 优秀 H5 案例收集 vol.1(不定期更新)

热门文章

  1. linux sudo root 权限绕过漏洞(CVE-2019-14287)
  2. 前端与算法 leetcode 350. 两个数组的交集 II
  3. MySQL集群搭建详解(三种结点分离)
  4. StringBuffer 和 StringBuilde
  5. Python基本数据结构之文件操作
  6. Vue基础系列(二)——Vue中的methods属性
  7. docker简介及安装
  8. Asciinema:你的所有操作都将被录制
  9. [内部类] java笔记之内部类
  10. Git常用命令(基础)