toString method for inherited case class in Scala

为君一笑 提交于 2020-05-16 06:32:06

问题


I am facing some inconsistency in calling toString method for case-classes in Scala. The first code sample:

case class Person(name: String, age: Int)
val jim = Person("jim", 42)
println(jim)

output: Person(jim,42)

For the next code sample I used a case class that extends Exception:

case class JimOverslept(msg: String) extends Exception
try {
  throw JimOverslept(msg = "went to bed late")
} catch {
  case e: JimOverslept => println(e)
}

output: playground.CaseClassOutput$JimOverslept

Actually, I would prefer the output like JimOverslept(went to bed late)

What is the reason the both outputs are so different? And what is the best way to obtain the output looks like desired one (JimOverslept(went to bed late))


回答1:


According to SLS 5.3.2 Case Classes

Every case class implicitly overrides some method definitions of class scala.AnyRef unless a definition of the same method is already given in the case class itself or a concrete definition of the same method is given in some base class of the case class different from AnyRef.

Now toString is already provided by base class in

case class JimOverslept(msg: String) extends Exception

where Exception extends base Throwable which provides toString definition. Hence try providing an override within the case class itself like so

case class JimOverslept(msg: String) extends Exception {
  override def toString: String = scala.runtime.ScalaRunTime._toString(this)
}


来源:https://stackoverflow.com/questions/61446999/tostring-method-for-inherited-case-class-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!