Type-safe query builders in Scala revisited: shapeless
Not so long ago, I wrote a post about creating type-safe query builders in Scala from scratch. In it, I also suggested using shapeless library to do what was described. Now, I decided to write how it could be done. The code is in this repository.
Without going into much details, the problem was to provide a type-safe way to build queries (to an abstract database, for instance) with parameters of different types. Something like
val query = beginQuery() .addParameter[String]("param1") .addParameter[Int]("param2") .addParameter[Boolean]("param3") .build() // Compile: query.execute("some string", 1, false) // Won't compile: query.execute(42, true, "another string")