case-class

Does the order of alternatives in a Scala match expression matter in terms of performance?

纵然是瞬间 提交于 2020-06-11 17:06:29
问题 In particular with respect to pattern matching and case classes. Consider the following: abstract class Expr case class Var(name: String) extends Expr case class Number(num: Double) extends Expr case class UnOp(operator: String, arg: Expr) extends Expr case class BinOp(operator: String, left: Expr, right: Expr) extends Expr object Expr { def simplify(expr: Expr): Expr = expr match { // Some basic simplification rules... case UnOp("-", UnOp("-", e)) => simplify(e) // Double negation case BinOp

Does the order of alternatives in a Scala match expression matter in terms of performance?

筅森魡賤 提交于 2020-06-11 17:06:26
问题 In particular with respect to pattern matching and case classes. Consider the following: abstract class Expr case class Var(name: String) extends Expr case class Number(num: Double) extends Expr case class UnOp(operator: String, arg: Expr) extends Expr case class BinOp(operator: String, left: Expr, right: Expr) extends Expr object Expr { def simplify(expr: Expr): Expr = expr match { // Some basic simplification rules... case UnOp("-", UnOp("-", e)) => simplify(e) // Double negation case BinOp

toString method for inherited case class in Scala

∥☆過路亽.° 提交于 2020-05-16 06:33:32
问题 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

toString method for inherited case class in Scala

只愿长相守 提交于 2020-05-16 06:33:19
问题 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

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

Generically rewriting Scala case classes

ぐ巨炮叔叔 提交于 2020-01-14 13:36:30
问题 Is it possible to generically replace arguments in a case class? More specifically, say I wanted a substitute function that received a "find" case class and a "replace" case class (like the left and right sides of a grammar rule) as well as a target case class, and the function would return a new case class with arguments of the find case class replaced with the replace case class? The function could also simply take a case class (Product?) and a function to be applied to all arguments

Case Class default apply method

老子叫甜甜 提交于 2020-01-10 19:42:27
问题 Assuming we have the following case class: case class CasePerson(firstName: String) And we also define a companion object for it: object CasePerson { def apply() = new CasePerson( "XYZ" ) } Notice that in the example above I explicitly defined a companion object with an apply method, without defining the the default apply method: // This "default" apply has the same argument as the primary constructor of the case class def apply(firstName : String) = new CasePerson(firstName) Q: So where does

Case Class default apply method

家住魔仙堡 提交于 2020-01-10 19:42:12
问题 Assuming we have the following case class: case class CasePerson(firstName: String) And we also define a companion object for it: object CasePerson { def apply() = new CasePerson( "XYZ" ) } Notice that in the example above I explicitly defined a companion object with an apply method, without defining the the default apply method: // This "default" apply has the same argument as the primary constructor of the case class def apply(firstName : String) = new CasePerson(firstName) Q: So where does

Scala trait implementation

試著忘記壹切 提交于 2020-01-06 06:46:37
问题 I have a case class: case class EvaluateAddress(addressFormat: String, screeningAddressType: String, value: Option[String]) { } This was working fine until I have a new use case where "value" parameter can be a class Object instead of String. My initial implementation to handle this use case: case class EvaluateAddress(addressFormat: String, screeningAddressType: String, addressId: Option[String], addressValue: Option[MailingAddress]) { @JsonProperty("value") def setAddressId(addressId:

Shapeless: restricting case class types

心已入冬 提交于 2020-01-05 01:37:14
问题 (NOTE: split from Shapeless: Trying to restrict HList elements by their type and Shapeless: own HList constraint using Coproduct ) Question 3 - restrict case classes by parameter types A very nice additional gain would be, if I could use HList constraints to constrain case class only to be built from AnyVals, Strings, and a specific MyBaseTrait, that recursively fulfill the same constraint. The constraint being defined on the base trait and not to have to touch any derived case class would be