如果泛型类型已经修复,如何用隐式参数覆盖泛型方法? [英] How to override a generic method with implicit arguments if the generic type is already fixed?

查看:126
本文介绍了如果泛型类型已经修复,如何用隐式参数覆盖泛型方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着重写这个方法

  def sum [B>:A](implicit num:Numeric [B] ):B = ... 

在类型 A 已经被固定为 Int



我已经试过了

 重写def sum:Int = ... 

但当然这并不会覆盖,导致在运行时基于动态类型的不同方法解析。



更进一步,

  def sum [B>:Int](implicit num:Numeric [B]):Int 

不覆盖,而

  def sum [B> ;:Int](implicit num:Numeric [Int]):Int 

不会,以及

  def sum(implicit num:Numeric [Int]):Int 

为何如此?是否有可能摆脱多余的界限 B



我不确定哪些类型和暗示我可以离开,并留下什么,以便该方法仍然覆盖。

解决方案

好吧,试图解释为什么规则必须强制您使用隐式参数和方差保留签名。首先,一个不明确的参数仍然是一个参数,它可以被明确地传递,除了可能当它有一个单例类型(这不会很有用)时,几个不同的它的实例是可能的。



假设我创建了

  case class ZModulo(val p:Int )扩展Numeric [Int] {
def plus(a:Int,b:Int)=(a + b)%p
//沿同一行的其他元素
}

它看起来像一个正确的 Numeric 数字文档没有说明应该采用哪种法律,但是 ZModulo 并非不合理。



现在您的

 类Summable [A] {
def sum [B>:A](implicit num:Numeric [A]):B = ...
}

如果我有 val ints:Summable [Int] ,我当然可以调用 ints.Sum (ZModulo(3))。因此,如果你的类是 Summable [Int] 的子类,它必须允许我这样做。所以你不能删除 Numeric 参数。

其次,假设我有一个数字[Any] 。不知道我该如何合理地为数字做这件事,但规范和编译器无法知道这一点。无论如何,他们也必须接受不合理的实施。所以,让我们来看看

 对象MixThemAll:Numeric [A] {...} 

Summable中的签名允许 ints.sum(MixThemAll)。所以你的子类也必须允许。



因此,让您在子类中删除隐式参数或方差是不合适的。


I try to override this method

def sum[B >: A](implicit num: Numeric[B]): B = ...

in a subclass where type A is already fixed to Int.

I already tried

override def sum: Int = ...

but this doesn't override of course, leading to different method resolution based on the dynamic type at runtime.

Going further,

def sum[B >: Int](implicit num: Numeric[B]): Int

does override, while

def sum[B >: Int](implicit num: Numeric[Int]): Int

does not, as well as

def sum(implicit num: Numeric[Int]): Int

Why is that the case? Is it at leats possible to get rid of the superfluous bound B?

I'm not sure which types and implicits I can leave out and what has to stay so that the method still overrides.

解决方案

Ok, trying to explain why the rules must force you to keep the signature with implicit parameter and variance.

First, animplicit argument is still an argument, it can be passed explicitely, and except maybe when it has a singleton type (which would not be very useful), several different instances of it are possible.

Suppose I create

case class ZModulo(val p: Int) extends Numeric[Int] {
  def plus(a: Int, b: Int) = (a+b) % p
  // others along the same line
}

It seems like a proper Numeric. Numeric documentation does not say which laws should be expected, but ZModulo is not unreasonable.

Now there is your

class Summable[A] {
  def sum[B >: A](implicit num: Numeric[A]): B =...
}

If I have val ints : Summable[Int], I am certainly allowed to call ints.Sum(ZModulo(3)). So if your class is to be a subclass of Summable[Int], it must allow me that. So you cannot remove the Numeric parameter.

Second, suppose I come with a Numeric[Any]. Not sure how I could do that reasonably for a numeric, but the spec and the compiler can't know that. And anyway, they must accept unreasonable implementations too. So let's have

object MixThemAll : Numeric[A] {...}

The signature in Summable allows ints.sum(MixThemAll). So your subclass must allow that too.

So letting you remove either implicit parameter or variance in the subclass would be unsound.

这篇关于如果泛型类型已经修复,如何用隐式参数覆盖泛型方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆