I want to write a case class that can take in a function that has one or more Ints as its arguments. For instance, these would be valid functions:
def foo(x: Int): String = "foo"
def bar(x: Int, y: Int): String = "bar"
def foobar(x: Int, y: Int, z: Int): String = "foobar"
but this would not:
def nonExample():String = "no"
The problem is that I can't get the right argument type for my case class.
case class Mine(function: ???) {}
I've tried:
case class Mine(function: (Int*) => String)
and this didn't work since (Int*) is a sequence of Ints. I also tried using Function and Function1, but that didn't work either. Any ideas (or alternatives if this isn't possible in Scala)?
Edit: As Didier Dupont mentioned, Mine also needs to know how many arguments the method requires. Above I oversimplified Mine. It will also take another argument that tells about the function passed in. Based on that, it'll decide how many parameters to pass into the function. But other than that chunk of code, everything else in Mine operates the same regardless of the function.
Function1,Function2, etc.. don't share a useful common trait, so I have serious doubts about it working without some magic.Eithertype. It hasLeftandRightas its possible values, each only supposed to hold a single value. It doesn't make a whole lot of sense to pattern match against something that has multiple one-parameter versions of itself, as it all ends up looking the same after erasure