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.
Problem reminder
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
1 2 3 4 5 6 7 8 9 10 |
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") |