Please log in to watch this conference skillscast.
Case classes are one of Scala's main selling points. For escapees from Java they offer a massive reduction in boilerplate and increase in correctness relative to the code that would typically have to be written by hand to achieve the same effect. For people coming to Scala from the Haskell world they correspond to the well behaved algebraic data types which are the bread and butter of modern functional programming.
But not everything is rosy. Case classes come with restrictions, some of which are essential, some not. And they have some characteristics, again some essential, some not, which are not always entirely appropriate or desirable. This has led to the often expressed desire to be able to pick and choose or otherwise customize case class features: case classes a la carte.
Unfortunately this has seemed to require changes to the Scala compiler which for various reasons have been perceived to be too difficult or too risky to attempt. The arrival of macros, macro annotations in particular, briefly raised the prospect that case classes a la carte might again be on the table. But macro annotations are not yet available in a production ready Scala compiler and might never be.
But all is not lost. Generating the scaffolding for case classes is a generic programming problem, and the fact that one solution to that problem is hard coded into scalac doesn't preclude other approaches from being applied. To that end I will demonstrate one way of tackling this problem using the generic programming library shapeless, and show some of the exciting new possibilities this opens up ... crucially in the context of current (ie. 2.10.x and 2.11.x) Scala compilers.
YOU MAY ALSO LIKE:
- Scala eXchange London 2019 (in London on 12th - 13th December 2019)
- Shapeless is Dead! Long Live Shapeless! (SkillsCast recorded in December 2017)
- Lightbend Akka for Scala - Professional (in London on 11th - 12th November 2019)
- Advanced Scala with Dick Wall (in London on 9th - 11th December 2019)
- Scalax2gether Community Day 2019 (in London on 14th December 2019)
- Don’t keep it to yourself - openness and honesty in the workplace (in London on 30th October 2019)
- Keynote by Naoki Takezoe on Revisit Dependency Injection in Scala and Introduction to Airframe (in London on 25th November 2019)
- Abstract Data Types In The Region Of Abysmal Pain, And How To Navigate Them (SkillsCast recorded in September 2019)
- The Last Frontier and Beyond (SkillsCast recorded in August 2019)
Case classes a la carte with shapeless, now!
Miles has been doing stuff with Scala for more than ten years, currently with Underscore Consulting. He is a cofounder of Typelevel and his best known project, the Scala generic programming library shapeless, is the weapon of choice wherever boilerplate needs to be scrapped or arities abstracted over.