首页 > 解决方案 > 如何知道分配了哪个功能?

问题描述

如果我有这样的事情:

def fn1(a:Integer)="1"
def fn2(a:Integer)="2"
val f=fn2 _
if( f== fn1 _) "1" else if(f==fn2 _) "2" else "other"

始终返回“其他”。如何在代码中知道哪个函数分配给了 f?

标签: scalalambdaequality

解决方案


这也有效:

def fn1(a:Integer)="1"
def fn2(a:Integer)="2"

val f1=fn1 _
val f2=fn2 _
val f=f2
f match {
case `f1` => "1"
case `f2` => "2"
case _ => "other"
}

推荐阅读