As described here
Type-safe way to divide a tuple into multiple tuples
I have a method with the following signature
def execute[T <: Record](funs: Seq[(Session) => T]): Seq[T]
where Session
is a Slick database session; the basic implementation of this method is
def execute[T <: Record](funs: Seq[(Session) => T): Seq[T] = {
db withSession {
session: Session => funs.map(fun => fun(session))
}}
(where db
is a Slick Database
) with other implementations that add things like logging, caching, multi-threading, etc. In particular, the multi-threading implementation uses funs.grouped(ceil(funs.size / threadCount)).map(funs => Future {})
to divide up the functions among several threads.
I would like to create a version of the method that accepts a tuple of functions so that I can return values of different types - as described in the question linked to above I didn't know of a good way to split up a tuple into smaller tuples and then recombine the results for the multi-threaded case, but the answer to that question was to use the Shapeless library's HList
s - however I am unclear as to how I can create a polymorphic variant of the (Session) => T
function, the problem being that all of the examples of polymorphic functions that I've seen use wrapped type params, e.g. (Set ~> Option)
which each wrap a polymorphic T
, but I am trying to create a (Session ~> T)
function where Session
is invariant and the polymorphic T
isn't wrapped in a Set
or Option
etc. I am doubtless looking at this problem the wrong way owing to not having sufficient experience with Shapeless.
How can I use Shapeless to create a polymorphic version of the def execute(funs: Seq[(Session) => T]): Seq[T]
function?
execute
here when you remove theToList
? There are other ways to enforce the constraint, but I'm not sure why this one isn't working. – Travis Brown