Scala: require that a function argument is a member of some class?

こ雲淡風輕ζ 提交于 2020-01-06 08:18:15

问题


I want to do something like

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

where f is supposed to be a member function of class A. I believe the standard solution is

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

and use it in this way

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

But now I can pass in an arbitrary function that has this signature, which may be not desired. Is there anyway to ensure that, the function I pass in is a member of certain class?


回答1:


Well, if you don't mind a few contortions, you can use the fact that a function IS a class after all...

// 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

now you can do:

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

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

but foo will not accept Int => String signatures

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


来源:https://stackoverflow.com/questions/13730926/scala-require-that-a-function-argument-is-a-member-of-some-class

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