Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查变量在Scala中的匿名函数_Scala_Anonymous Function - Fatal编程技术网

检查变量在Scala中的匿名函数

检查变量在Scala中的匿名函数,scala,anonymous-function,Scala,Anonymous Function,我有一个场景,其中一个匿名函数存储在var中 val x = Integer.max _ 我想做一些逻辑,比如 if(x == Integer.max _){ println("Using the max function") } 但我注意到这些匿名函数从来都不相等 val x = Integer.max _ val y = Integer.max _ println(x==y) //false println(x eq y) //false 那么,我是否可以检查我的匿名函

我有一个场景,其中一个匿名函数存储在var中

val x = Integer.max _
我想做一些逻辑,比如

if(x == Integer.max _){
    println("Using the max function")
}
但我注意到这些匿名函数从来都不相等

val x = Integer.max _
val y = Integer.max _

println(x==y)   //false 
println(x eq y) //false

那么,我是否可以检查我的匿名函数;如果我不知道模仿这个功能的最佳方式是什么?

AFAICT没有干净的方法。问题是Scala将所有匿名函数编译成匿名类-因此不同的匿名函数得到不同的类(因此为什么
==
表明它们是不同的)


否则,您可能会尝试验证这两个函数的匿名类在结构上是否相同,但结果可能会非常令人不快。

谢谢,这对我来说是可行的。不过,如果scala每次都将相同的函数编译到相同的类中,那就太好了。
scala> val x: (Int, Int) => Int = Integer.max
x: (Int, Int) => Int = <function2>

scala> x.getClass
res: Class[_ <: (Int, Int) => Int] = class $anonfun$1
val max: (Int, Int) => Int = Integer.max
...
val x = max
...
if (x == max) {
    println("Using the max function")
}