代码之家  ›  专栏  ›  技术社区  ›  Francis

函数返回的Gatling exec函数

  •  3
  • Francis  · 技术社区  · 7 年前

    我正在使用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
    
    1 回复  |  直到 7 年前
        1
  •  5
  •   Eugene Loy    7 年前

    根据do docs :

    …这是 alias for :

    Session => Validation[T]
    

    exec 某种类型的东西 Session => Session Expression[Session] 存在于范围中),并且您得到编译错误。

    在第二种情况下,您将占位符/部分应用程序用于:

    justDoIt("hello world")(_)
    

    …相当于:

    x => justDoIt("hello world")(x)
    

    Session Validation[Session] 表达式[会话] 这就是为什么

    更新: 这是 relevant implicit 这就是转换。

    更新2: justDoIt

    def justDoIt(param: String): Session => Validation[Session] = s => s.set("some", param)