I have a series of functions that accept a function as an argument (func
), and the only difference in them is that func
accepts different numbers of parameters.
def tryGet[T,A](func: (A) => T)
def tryGet[T,A,B](func: (A,B) => T)
... [T,A,B,C...]
Is there a way to make a function accept a function with any length argument list?
I tried using variable length argument lists, eg func:(A*)
but had trouble passing any functions in as it now requires a Seq[A]
.
func()
. For example if I wanted to pass in the functiondef getSomething(int:Int) = Future.successful(int)
, by calling tryGet like sotryGet( (int:Int) => getSomething(int) )
I was usingfunc() map { ... handle exceptions and return an Option instead}
. The wider question i'm trying to solve is can I wrap a load of API calls that throw exceptions, so that they throw Options instead in the case of NoSuchElementExceptions. – BrusselstryGet
shouldn't know or care what the function is, it only knows that iffunc
throws some specific exceptions it should returnNone
, and if it doesn't then it should returnSome(T)
– Brusselsfunc()
to compile? If it may take any number of arguments? You do not really want to pass any kind of function. But rather any kind o block that results onFuture[T]
and call something over it. Something likedef tryGet[T](block: => Future[T]): Future[Option[T]] = block.recoverWith(...)
. – DejectaA => T
where A is any kind of product. Thus, if you need two arguments, you pass them as a tuple. – Dejecta