I was trying to do some stuff last night around accepting and calling a generic function (i.e. the type is known at the call site, but potentially varies across call sites, so the definition should be generic across arities).
For example, suppose I have a function f: (A, B, C, ...) => Z
. (There are actually many such f
s, which I do not know in advance, and so I cannot fix the types nor count of A, B, C, ..., Z
.)
I'm trying to achieve the following.
How do I call
f
generically with an instance of(A, B, C, ...)
? If the signature off
were known in advance, then I could do something involvingFunction.tupled f
or equivalent.How do I define another function or method (for example, some
object
'sapply
method) with the same signature asf
? That is to say, how do I define ag
for whichg(a, b, c, ...)
type checks if and only iff(a, b, c, ...)
type checks? I was looking into Shapeless'sHList
for this. From what I can tell so far,HList
at least solves the "representing an arbitrary arity args list" issue, and also, Shapeless would solve the conversion to and from tuple issue. However, I'm still not sure I understand how this would fit in with a function of generic arity, if at all.How do I define another function or method with a related type signature to
f
? The biggest example that comes to mind now is someh: (A, B, C, ...) => SomeErrorThing[Z] \/ Z
.
I remember watching a conference presentation on Shapeless some time ago. While the presenter did not explicitly demonstrate these things, what they did demonstrate (various techniques around abstracting/genericizing tuples vs HList
s) would lead me to believe that similar things as the above are possible with the same tools.
Thanks in advance!