I'm working with Gatling using Scala IDE (Eclipse Luna) and I ran into this issue that I would like to understand.
I have this function
import io.gatling.core.session.Session
import io.gatling.core.Predef._
object Predef {
def justDoIt(param: String): Session => Session = s => s.set("some", param)
}
And I'm trying to use it that way
val testScenario1 = scenario("Test")
.exec(justDoIt("hello world"))
val testScenario2 = scenario("Test")
.exec(justDoIt("hello world")(_))
For some reason, only the latter compiles. The former is complaining about the overload not applicable on argument of type Session => Session.
I would like to understand the difference between those two lines and why is the first one failing to compile.
I also did the following test and both syntax seems to be doing the same thing:
scala> def hello(fn: String => String) = fn("Hello")
hello: (fn: String => String)String
scala> def message(name: String): String => String = greeting => s"$greeting $name"
message: (name: String)String => String
scala> hello(message("world"))
res1: String = Hello world
scala> hello(message("world")(_))
res2: String = Hello world
According do docs:
exec can also be passed an Expression function.
... which is alias for:
Session => Validation[T]
Also pay attention to this excerpt from the first link:
For those who wonder how the plumbing works and how you can return a Session instead of Validation[Session] in the above examples, that’s thanks to an implicit conversion.
Now, with that info you can see that in the first case you are passing to exec something of type Session => Session (for which there is no implicit conversion to Expression[Session] exists in scope) and you get compile error.
In the second case you're using placeholder/partial application for:
justDoIt("hello world")(_)
... which is equivalent of:
x => justDoIt("hello world")(x)
... which can benefit from implicit conversion of the return value (from Session to Validation[Session]) and, as a result whole expression gets inferred to be conforming Expression[Session] which is just what exec requires.
UPDATE: here is the relevant implicit that does the conversion.
UPDATE 2: to make your original example work you can simply change the return type of justDoIt like this (to benefit from implicit conversion):
def justDoIt(param: String): Session => Validation[Session] = s => s.set("some", param)
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