Scala:参数化类的重写 equals 函数的擦除问题
我无法很好地理解如何使用清单。
这就是我的问题: 我创建了一个新的参数化类 C 并尝试像这样重写 equals:
override def equals(that:Any)=that match{
case that:C[T] => true /*do smth else not relevant*/
case _ => false
}
当然,我收到“警告:类型模式 C[T] 中的非变量类型参数 T 未经检查,因为它已被删除”。我尝试使用清单,就像我在许多其他函数中使用的那样:
override def equals(that:Any)(implicit manifest:Manifest[T])=that match{
case that:C[T] => true
case _ => false
}
但我收到了“错误:方法等于覆盖任何内容”消息。
我不知道如何解决这个问题。有人可以帮我吗?
I'm having troubles on understanding well how to use manifests.
That's my problem:
I've creat a new parametrized class C and tryed to override equals like this:
override def equals(that:Any)=that match{
case that:C[T] => true /*do smth else not relevant*/
case _ => false
}
Of course I recieve the "warning: non variable type-argument T in type pattern C[T] is unchecked since it is eliminated by erasure". I tryied so using manifests like I was using in many other functions:
override def equals(that:Any)(implicit manifest:Manifest[T])=that match{
case that:C[T] => true
case _ => false
}
But I recieved the "error: method equals overrides nothing" message.
I don't know how to fix this. Could anyone please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你无法修复它。欢迎享受与 java 顺畅互操作的乐趣。改进 def equals(x: Any): Boolean 的唯一方法是编写不同的方法。
我总是试图说服马丁,我们应该以不同的方式实现 == 脱糖,目标是像“def DecentEquals[T](x: T)(implicit equals: Equiv[T])”这样的东西,使用默认的隐式和桥接方法来实现除非你关心,否则它是无缝的,但他认为平等测试不应该变得更慢。
You can't fix it. Welcome to the joys of smooth interoperation with java. The only way to improve equals from
def equals(x: Any): Boolean
is to write a different method.I'm always trying to convince martin that we should implement == desugaring differently, aiming at something like "def decentEquals[T](x: T)(implicit equiv: Equiv[T])" with default implicits and bridge methods to make it seamless unless you care, but he thinks equality tests shouldn't get any slower.
只是@extempore答案的补充;完全有可能编写类型安全的空安全 equals 方法。看看 Scalaz< /strong>
身份
(和示例)基于类型,它需要任何参数化也相等(即
C[T] == C[U] iff T =:= U
当然,尝试重写方法的问题是:
这样:
这不构成重写,因为该方法具有不同的签名。而是重载该方法,这就是为什么scala需要
override
修饰符:否则你可能没有注意到代码没有按照你想象的那样做!Just an adjunct to @extempore's answer; it is entirely possible to write null-safe equals methods which may be type safe. Have a look at Scalaz
Identity
(and examples)Being based on types, it would require any parameterization to be equal also (i.e.
C[T] == C[U] iff T =:= U
Of course, the problem with trying to override a method:
With this:
Is that this does not constitute overriding. Because the method has a different signature you have overloaded the method instead. Which is why it's nice that scala requires the
override
modifier: otherwise you might not have noticed that the code was not doing what you thought it was!会编译,但我不认为它能达到你想要的效果。您试图查看类型参数是否匹配(我假设),但我不知道如何(或是否)可以做到这一点。 (编辑:阅读更多文献,我认为你不能。出于明显的原因,你不能用清单感知函数覆盖非清单感知函数)。
will compile, but I don't think it does what you want. You're trying to see if the type-parameters match (I assume), but I don't know how (or if) you can do that. (Edit: reading more of the literature, I think you cannot. You cannot, for obvious reasons, override a non-manifest-aware function with a manifest-aware one).