函数返回的 Gatling exec 函数
Gatling exec function returned by function
我正在使用 Scala IDE (Eclipse Luna) 与 Gatling 合作,我 运行 进入了这个我想了解的问题。
我有这个功能
import io.gatling.core.session.Session
import io.gatling.core.Predef._
object Predef {
def justDoIt(param: String): Session => Session = s => s.set("some", param)
}
我正在尝试以这种方式使用它
val testScenario1 = scenario("Test")
.exec(justDoIt("hello world"))
val testScenario2 = scenario("Test")
.exec(justDoIt("hello world")(_))
出于某种原因,只有后者可以编译。前者抱怨过载不适用于 Session => Session
.
类型的参数
我想了解这两行之间的区别以及为什么第一行编译失败。
我也做了以下测试,两种语法似乎都在做同样的事情:
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
根据docs:
exec can also be passed an Expression function.
... 即 alias for:
Session => Validation[T]
另请注意第一段的这段摘录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.
现在,根据该信息,您可以看到在第一种情况下,您传递给 exec
类型 Session => Session
的内容(不存在到 Expression[Session]
的隐式转换在范围内),你会得到编译错误。
在第二种情况下,您正在使用 placeholder/partial 申请:
justDoIt("hello world")(_)
... 相当于:
x => justDoIt("hello world")(x)
... 可以受益于 return 值的隐式转换(从 Session
到 Validation[Session]
),因此整个表达式被推断为符合 Expression[Session]
这正是 exec
所要求的。
更新: 这里是进行转换的 relevant implicit。
更新 2: 要使您的原始示例正常工作,您可以像这样简单地更改 justDoIt
的 return 类型(以从隐式转换中获益) :
def justDoIt(param: String): Session => Validation[Session] = s => s.set("some", param)
我正在使用 Scala IDE (Eclipse Luna) 与 Gatling 合作,我 运行 进入了这个我想了解的问题。
我有这个功能
import io.gatling.core.session.Session
import io.gatling.core.Predef._
object Predef {
def justDoIt(param: String): Session => Session = s => s.set("some", param)
}
我正在尝试以这种方式使用它
val testScenario1 = scenario("Test")
.exec(justDoIt("hello world"))
val testScenario2 = scenario("Test")
.exec(justDoIt("hello world")(_))
出于某种原因,只有后者可以编译。前者抱怨过载不适用于 Session => Session
.
我想了解这两行之间的区别以及为什么第一行编译失败。
我也做了以下测试,两种语法似乎都在做同样的事情:
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
根据docs:
exec can also be passed an Expression function.
... 即 alias for:
Session => Validation[T]
另请注意第一段的这段摘录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.
现在,根据该信息,您可以看到在第一种情况下,您传递给 exec
类型 Session => Session
的内容(不存在到 Expression[Session]
的隐式转换在范围内),你会得到编译错误。
在第二种情况下,您正在使用 placeholder/partial 申请:
justDoIt("hello world")(_)
... 相当于:
x => justDoIt("hello world")(x)
... 可以受益于 return 值的隐式转换(从 Session
到 Validation[Session]
),因此整个表达式被推断为符合 Expression[Session]
这正是 exec
所要求的。
更新: 这里是进行转换的 relevant implicit。
更新 2: 要使您的原始示例正常工作,您可以像这样简单地更改 justDoIt
的 return 类型(以从隐式转换中获益) :
def justDoIt(param: String): Session => Validation[Session] = s => s.set("some", param)