Looser coupling with type classes

So far, we have been reading and writing simple types to the database. Let's imagine that we want to add a gender column to our database. We will store the gender as an enumeration in our physicists database. Our table is now as follows:

mysql> CREATE TABLE physicists (
        id INT(11) AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(32) NOT NULL,
        gender ENUM("Female", "Male") NOT NULL
);

How can we represent genders in Scala? A good way of doing this is with an enumeration:

// Gender.scala

object Gender extends Enumeration {
  val Male = Value
  val Female = Value
}

However, we now have a problem when deserializing objects from the database: JDBC has no built-in mechanism to convert from a SQL ENUM type to a Scala Gender type. ...

Get Scala: Guide for Data Science Professionals now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.