如何在 Scala 中链接函数参数? [英] How to chain function arguments in Scala?

查看:47
本文介绍了如何在 Scala 中链接函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下函数定义我的测试(暂时忽略一些依赖项,例如 fixture1 等,它们位于底部):

I'd like to define my tests using the following function (ignore some dependencies for now e.g. fixture1 etc they are at the bottom):

multiTest("my test name", fixture1) { case (x: Double, y: Int, z: String) =>
   // test body
}

multiTest 在我的基本自定义 FunSpecLike 子类中定义为:

and multiTest is defined in my base custom FunSpecLike subclass as:

def multiTest(testName: String, fixture: FixtureTable)(fun: => Unit)(implicit pos: source.Position): Unit = {
    val heading = fixture.heading
    fixture.values.foreach { tuple =>
        it(autoGenerateDesc(heading, tuple)) {
            fun tuple // <<<<<< how can I pass the tuple to the definition above?
        }
    }
}

如何将元组推送到函数?

How can I push the tuple to the function?

一些缺失的部分是:

case class FixtureTable(heading: Map[String, String], values: Seq[Any])
// tableFor generates the permutations of all paramater values 
val fixture1 : FixtureTable = tableFor(
  ("x", List(1e-1, 1e-2)),
  ("y", List(1, 2, 3)),
  ("z", List("a", "b")))

推荐答案

事实上,你不能.将 multitest 改为

As it is, you can't. Change multitest to

def multiTest(testName: String, fixture: FixtureTable)(fun: Any => Unit)(implicit pos: source.Position): Unit = {
    val heading = fixture.heading
    fixture.values.foreach { tuple =>
        it(autoGenerateDesc(heading, tuple)) {
            fun(tuple)
        }
    }
}

甚至更好

case class FixtureTable[A](heading: Map[String, String], values: Seq[A])
def multiTest[A](testName: String, fixture: FixtureTable[A])(fun: A => Unit)(implicit pos: source.Position): Unit = {
    val heading = fixture.heading
    fixture.values.foreach { tuple =>
        it(autoGenerateDesc(heading, tuple)) {
            fun(tuple)
        }
    }
}

但是在这种情况下编写 tableFor 函数会更困难.

but it would be harder to write the tableFor function for this case.

这篇关于如何在 Scala 中链接函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆