How to chain function arguments in Scala?

大兔子大兔子 提交于 2020-01-17 05:20:07

问题


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
}

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?

Some of the missing pieces are:

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")))

回答1:


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)
        }
    }
}

or even better

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)
        }
    }
}

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



来源:https://stackoverflow.com/questions/39744322/how-to-chain-function-arguments-in-scala

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!