How is this case class match pattern working?
When doing pattern matching, the Scala compiler will interpret o1 c1 o2 the same as c1(o1, o2). That’s why :: works inside pattern matches too.
When doing pattern matching, the Scala compiler will interpret o1 c1 o2 the same as c1(o1, o2). That’s why :: works inside pattern matches too.
First for some shameless self-promotion: Eugene Burmako and I are giving a talk on type providers, a closely related topic, at Scalar 2014 tomorrow, and I encourage you to take a look at the example project we put together for the talk if you’re interested in this kind of thing. While type macros are no … Read more
MethodSymbol has an isCaseAccessor method that allows you to do precisely this: def getMethods[T: TypeTag] = typeOf[T].members.collect { case m: MethodSymbol if m.isCaseAccessor => m }.toList Now you can write the following: scala> case class Person(name: String, age: Int) defined class Person scala> getMethods[Person] res1: List[reflect.runtime.universe.MethodSymbol] = List(value age, value name) And you get only … Read more
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
This is a fundamental feature of object-oriented programming that doesn’t get as much attention as it deserves. Suppose you have a collection C[+T]. What the +T means is that if U <: T, then C[U] <: C[T]. Fair enough. But what does it mean to be a subclass? It means that every method should work … Read more
Overloading constructors isn’t special for case classes: case class Foo(bar: Int, baz: Int) { def this(bar: Int) = this(bar, 0) } new Foo(1, 2) new Foo(1) However, you may like to also overload the apply method in the companion object, which is called when you omit new. object Foo { def apply(bar: Int) = new … Read more
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
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
Note that this can be done much more elegantly without the toString / c.parse business: import scala.language.experimental.macros abstract class Model { def toMap[T]: Map[String, Any] = macro Macros.toMap_impl[T] } object Macros { import scala.reflect.macros.Context def toMap_impl[T: c.WeakTypeTag](c: Context) = { import c.universe._ val mapApply = Select(reify(Map).tree, newTermName(“apply”)) val pairs = weakTypeOf[T].declarations.collect { case m: MethodSymbol … Read more
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