Scala case class inheritance

My preferred way of avoiding case class inheritance without code duplication is somewhat obvious: create a common (abstract) base class: abstract class Person { def name: String def age: Int // address and other properties // methods (ideally only accessors since it is a case class) } case class Employer(val name: String, val age: Int, … Read more

What are the disadvantages to declaring Scala case classes?

First the good bits: Everything immutable by default Yes, and can even be overridden (using var) if you need it Getters automatically defined Possible in any class by prefixing params with val Decent toString() implementation Yes, very useful, but doable by hand on any class if necessary Compliant equals() and hashCode() Combined with easy pattern-matching, … Read more

Easy idiomatic way to define Ordering for a simple case class

My personal favorite method is to make use of the provided implicit ordering for Tuples, as it is clear, concise, and correct: case class A(tag: String, load: Int) extends Ordered[A] { // Required as of Scala 2.11 for reasons unknown – the companion to Ordered // should already be in implicit scope import scala.math.Ordered.orderingToOrdered def … Read more

IntelliJ Scala Plugin’s case class indentation is absurd

Try File -> Settings… -> Code Style -> Scala There are lots of settings to customize your code formatting in there. In the “Wrapping and Braces” tab, under “Method declaration parameters”: check “use normal indent for parameters” uncheck “Align when multiline” This will change it to the example you provided. If you want it to … Read more

Case objects vs Enumerations in Scala

One big difference is that Enumerations come with support for instantiating them from some name String. For example: object Currency extends Enumeration { val GBP = Value(“GBP”) val EUR = Value(“EUR”) //etc. } Then you can do: val ccy = Currency.withName(“EUR”) This is useful when wishing to persist enumerations (for example, to a database) or … Read more

What is the difference between Scala’s case class and class?

Case classes can be seen as plain and immutable data-holding objects that should exclusively depend on their constructor arguments. This functional concept allows us to use a compact initialization syntax (Node(1, Leaf(2), None))) decompose them using pattern matching have equality comparisons implicitly defined In combination with inheritance, case classes are used to mimic algebraic datatypes. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)