0

我想做类似的事情

class A {
  def f1: Unit = ...
  def f2: Unit = ...
}
def foo(f: => Unit) {
  (new A).f // ???
}

其中 f 应该是 A 类的成员函数。我相信标准解决方案是

def foo(f: A => Unit) {
  f(new A)
}

并以这种方式使用它

foo(_.f1)
foo(_.f2)

但现在我可以传入一个具有此签名的任意函数,这可能不是我们想要的。有没有办法确保我传入的函数是某个类的成员?

4

1 回答 1

2

好吧,如果你不介意一些扭曲,你可以使用函数毕竟是一个类的事实......

// abstract class MyIntToString extends (Int => String) // declare here if you want 
                                                       // to use from different classes

// EDIT: f1 and f2 are now val instead of def as per comment below
// by @Régis Jean-Gilles
class A {
    abstract class MyIntToString private[A]() extends (Int => String) 
           // if MyIntToString is declared here
           // with a constructor private to the enclosing class
           // you can ensure it's used only within A (credit goes to @AlexeyRomanov 
           // for his comment below)
    val f1 = new MyIntToString {
        def apply(i: Int) = i.toString + " f1"
    }
    val f2= new MyIntToString {
        def apply(i: Int) = i.toString + " f2"
    }
 }

def foo(f: A#MyIntToString) = f(42) // f: MyIntToString if MyIntToString not nested in A
val a = A

现在你可以这样做:

scala> foo((new A).f1)
res1: String = 42 f1

scala> foo((new A).f2)
res2: String = 42 f2

但 foo 不接受Int => String签名

scala> val itos = (i:Int) => i.toString
itos: Int => String = <function1>

scala> foo(itos)
<console>:11: error: type mismatch;
 found   : Int => String
 required: MyIntToString
              foo(itos)
                  ^
于 2012-12-05T21:12:23.850 回答