Using .tupled method when companion object is in class
You can also write (Person.apply _).tupled to avoid repeating the types.
You can also write (Person.apply _).tupled to avoid repeating the types.
This is very subjective of course, but in my opinion it is good practice to have exception classes as case classes. The main rationale being that when you catch an exception, you are doing pattern matching, and case classes are much nicer to use in pattern matching. Here’s an example that takes advantage of the … Read more
More recently (Oct 2016, six years after the OP), the blog post “Scala and 22” from Richard Dallaway explores that limit: Back in 2014, when Scala 2.11 was released, an important limitation was removed: Case classes with > 22 parameters are now allowed. That said, there still exists a limit on the number of case … Read more
Check out a small extensions library named sext. It exports these two functions exactly for purposes like that. Here’s how it can be used for your example: object Demo extends App { import sext._ case class ClassDecl( kind : Kind, list : List[ VarDecl ] ) sealed trait Kind case object Complex extends Kind case … Read more
As my professor used to say, only the code tells the truth! So just take a look at the code that is generated for: case class A(i: Int, s: String) We can instruct the Scala compiler to show us the generated code after the different phases, here after the typechecker: % scalac -Xprint:typer test.scala [[syntax … Read more
How about calling unapply().get in the companion object? case class Foo(foo: String, bar: Int) val (str, in) = Foo.unapply(Foo(“test”, 123)).get // str: String = test // in: Int = 123
It is not redundant in the sense that using it does change things. As one would expect, you cannot extend a final case class, but you can extend a non-final one. Why does wartremover suggest that case classes should be final? Well, because extending them isn’t really a very good idea. Consider this: scala> case … Read more
One word: equality case classes come with a supplied implementation of equals and hashCode. The equivalence relation, known as equals works like this (i.e. must have the following properties): For all x; x equals x is true (reflexive) For x, y, z; if x equals y and y equals z then x equals z (transitive) … Read more
This should work: def getCCParams(cc: AnyRef) = cc.getClass.getDeclaredFields.foldLeft(Map.empty[String, Any]) { (a, f) => f.setAccessible(true) a + (f.getName -> f.get(cc)) }
The reason for the conflict is that the case class provides the exact same apply() method (same signature). First of all I would like to suggest you use require: case class A(s: String) { require(! s.toCharArray.exists( _.isLower ), “Bad string: “+ s) } This will throw an Exception if the user tries to create an … Read more