Let's say I have a function with a signature like this:
def tsadd(key: Any, time: Double, value: Any)(implicit format: Format): Option[Int]
And I want to create a list of some number of these functions for later evaluation. How do I do it. I tried creating a list like:
val pipelineCmds:List[(String,Double,Any)(Format) => Option[Int]] = Nil
and doing this:
pipelineCmds ++ r.tsadd(symbol, timestamp.getMillis.toDouble, value)
But the val didn't respond well the implicit param Format. It expects to see a ] after the first set of parens.
The ultimate goal is to do something like
r.pipeline { p =>
pipelineCmds.foreach(c => p.c)
}
Any help is greatly appreciated!
As far as I know, functions with implicit parameters are annoying to work with. The appropriate types are (your choice):
(String, Double, Any) => Format => Option[Int] // As written
Format => (String, Double, Any) => Option[Int] // Conceptually works more like this
String => Double => Any => Format => Option[Int] // Completely curried
(String, Double, Any, Format) => Option[Int] // Closest to how the JVM sees the method
but partial application of the function does not work well. You can annoyingly annotate all your types to get the last version:
class Format {} // So the example works
def tsadd(s: String, d: Double, a: Any)(implicit f: Format): Option[Int] = None
scala> tsadd(_: String, _: Double, _: Any)(_: Format)
res2: (String, Double, Any, Format) => Option[Int] = <function4>
but it's not much harder to write your own to be whatever shape you want:
def example(f: Format => (String, Double, Any) => Option[Int]) = f
scala> example(f => { implicit val fm=f; tsadd _ })
res3: (Format) => (String, Double, Any) => Option[Int] = <function1>
Of course, if you already know the implicit values when you're creating the list, you just need the type
(String, Double, Any) => Option[Int]
and you assign the functions like
tsadd _
scala> val pipelineCmds:List[(String,Double,Any) => Format => Option[Int]] = Nil
pipelineCmds: List[(String, Double, Any) => (Format) => Option[Int]] = List()
But note that the "implicitness" is lost from function values and you must explicitly pass in a format.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With