6

是否可以创建具有多个代码块的自定义控制结构,以before { block1 } then { block2 } finally { block3 }? 问题仅与糖部分有关-我知道通过将三个块传递给方法可以轻松实现该功能,例如doInSequence(block1, block2, block3).

一个真实的例子。对于我的测试实用程序,我想创建一个这样的结构:

getTime(1000) {
  // Stuff I want to repeat 1000 times.
} after { (n, t) => 
  println("Average time: " + t / n)
}

编辑

最后我想出了这个解决方案:

object MyTimer {
  def getTime(count: Int)(action : => Unit): MyTimer = {
    val start = System.currentTimeMillis()
    for(i <- 1 to count) { action }
    val time = System.currentTimeMillis() - start
    new MyTimer(count, time)
  }
}

class MyTimer(val count: Int, val time: Long) {
  def after(action: (Int, Long) => Unit) = {
    action(count, time)
  }
}

// Test
import MyTimer._

var i = 1
getTime(100) {
  println(i)
  i += 1
  Thread.sleep(10)
} after { (n, t) => 
  println("Average time: " + t.toDouble / n)
}

输出是:

1
2
3
...
99
100
Average time: 10.23

它主要基于Thomas Lockney的回答,我只是添加了伴随对象以便能够import MyTimer._

谢谢大家,伙计们。

4

4 回答 4

13

一般原则。您当然也可以使用 f 参数。(请注意,此示例中的方法名称没有意义。)

scala> class Foo {
     | def before(f: => Unit) = { f; this }
     | def then(f: => Unit) = { f; this }
     | def after(f: => Unit) = { f; this }
     | }
defined class Foo

scala> object Foo { def apply() = new Foo }
defined module Foo

scala> Foo() before { println("before...") } then {
     | println("then...") } after {
     | println("after...") }
before...
then...
after...
res12: Foo = Foo@1f16e6e
于 2011-01-01T10:06:26.303 回答
8

如果您希望这些块以特定顺序出现,则对 Knut Arne Vedaa 答案的更改将起作用:

class Foo1 {
  def before(f: => Unit) = { f; new Foo2 }
}

class Foo2 {
  def then(f: => Unit) = { f; new Foo3 }
}

...
于 2011-01-01T11:01:19.607 回答
3

对于您给定的示例,关键是让返回类型getTime具有after方法。根据上下文,您可以使用包含两种方法的单个类或特征。这是一个非常简化的示例,说明您如何处理它:

class Example() {
  def getTime(x: Int)(f : => Unit): Example = {
    for(i <- 0 to x) {
      // do some stuff
      f
      // do some more stuff
    }
    // calculate your average
    this
  }
  def after(f: (Int, Double) => Unit) = {
    // do more stuff
  }
}
于 2011-01-01T09:26:33.713 回答
1

不可能有“拆分”方法,但您可以模仿它。

class Finally(b: => Unit, t: => Unit) {
    def `finally`(f: => Unit) = {
        b
        try { t } finally { f }
    }
}

class Then(b: => Unit) {
    def `then`(t: => Unit): Finally = new Finally(b, t)
}

def before(b: => Unit): Then = new Then(b)

scala> before { println("Before") } `then` { 2 / 0 } `finally` { println("finally") }
Before
finally
[line4.apply$mcV$sp] (<console>:9)
(access lastException for the full trace)
scala>
于 2011-01-02T13:30:51.753 回答