Scala eq问题
我对Scala中的以下代码有问题Scala eq问题,scala,Scala,我对Scala中的以下代码有问题 import org.junit.Test import org.junit.Assert._ class BoxingTest { val holder: Holder[Integer] = new Holder[Integer](); @Test def Holder_Eq_Primitive(){ assertEquals(holder, holder eq 1); } @Te
import org.junit.Test
import org.junit.Assert._
class BoxingTest {
val holder: Holder[Integer] = new Holder[Integer]();
@Test
def Holder_Eq_Primitive(){
assertEquals(holder, holder eq 1);
}
@Test
def Holder_Eq_Boxed(){
assertEquals(holder, holder eq 1.asInstanceOf[Integer]);
}
}
class Holder[T] {
def eq(other: T): Holder[_] = this;
}
我在编译时遇到以下错误
/BoxingTest.scala:12: error: type mismatch;
[INFO] found : Int
[INFO] required: AnyRef
[INFO] Note: primitive types are not implicitly converted to AnyRef.
[INFO] You can safely force boxing by casting x.asInstanceOf[AnyRef].
[INFO] assertEquals(holder, holder eq 1);
[INFO] ^
[ERROR] one error found
[INFO] -------------------------
为什么从Int到Integer的隐式转换不能解决这个问题
我可以通过不使用eq轻松修复代码,但这似乎并不正确。IMHO此处应应用可用的隐式转换
更新
我用这样的签名解决了这个问题
import org.junit.Test
import org.junit.Assert._
class BoxingTest {
@Test
def Holder_Eq_Primitive(){
val holder: Holder[Int] = new Holder[Int]();
assertEquals(holder, holder eq 1);
}
@Test
def Holder_Eq_Boxed(){
val holder: Holder[Integer] = new Holder[Integer]();
assertEquals(holder, holder eq 1.asInstanceOf[Integer]);
}
}
class Holder[T] {
def eq(other: T): Holder[_] = ...;
}
不过,使用包装类型还是不错的。我尝试将
Int
值与整数文本进行比较,并从编译器中得到了一些有趣的提示。这可能有助于了解这种行为的原因
scala> val a = 1
scala> a eq 1
<console>:6: error: type mismatch;
found : Int
required: ?{val eq: ?}
Note that implicit conversions are not applicable because they are ambiguous:
both method int2Integer in object Predef of type (Int)java.lang.Integer
and method intWrapper in object Predef of type (Int)scala.runtime.RichInt
are possible conversion functions from Int to ?{val eq: ?}
a eq 1
^
scala>val a=1
scala>a等式1
:6:错误:类型不匹配;
找到:Int
必需:?{val eq:?}
请注意,隐式转换不适用,因为它们不明确:
类型为(Int)java.lang.Integer的对象Predef中的两个方法int2Integer
和(Int)scala.runtime.RichInt类型的对象Predef中的方法intWrapper
是从Int到{val eq:?}的可能转换函数
a等式1
^
我认为这是关于Java中的类型擦除。编译类时
class Holder[T] {
def eq(other: T): Holder[_] = this;
}
类型T
将被删除。您可以进行以下测试:
另一方面,Int
类是AnyVal
的子类型,而不是AnyRef
。因此,如果尝试使用参数1
应用方法eq
,该参数的类型为Int
,则会发出运行时错误
PS:Vilius Normantas指出,尽管
Int
可以隐式转换为java.lang.Integer
,但它也可以隐式转换为RichInt 你是在“我如何使我的代码工作”的背景下提出这个问题,还是在“我想知道Scala中这个设计决策的背景动机”?@Andrzej,两者兼而有之。我更新了我的问题。顺便说一句,你为什么使用Java的Integer
而不是Scala的Int
?@soc使用Int修复了这个问题,仍然是奇怪的行为。谢谢。@Jona我删除了误导性的实现。我的问题是关于一个不返回布尔值的方法签名。这是一个DSL用例。这是一个有趣的注释。我已经放弃使用情商,因为所有相关的麻烦。