English 中文(简体)
功能差异和重叠
原标题:Scala Function Variance and Overriding

在超负荷工作时,我对方法差异的认识不大。

而这种完美的工作则由于返回类型上的共担而得以实现。

class Bla 
class Fasel extends Bla 

trait Test[A] {
 def tester(): Bla = new Bla
}

class FooTest[A](a: A) extends Test[A] {
 override def tester(): Fasel = new Fasel                                      
}

this one fails even though functions are contravariant in their parameter types.

class Bla 
class Fasel extends Bla 

trait Test[A] {
 def tester(a: Fasel): Bla = new Bla                                           
}

class FooTest[A](a: A) extends Test[A] {
 override def tester(a: Bla): Fasel = new Fasel
}

我在这里错了吗? 任何要点?

Regards, raichoo

最佳回答

这里还有两点:

  1. A function and a method are not the same thing
  2. Methods are not polymorphic in their parameters types

页: 1 方法如下:而不是Function1>。 它可以使用 underscore子解除其职能:

val f = (new FooTest[String]).tester _ // Fasel => Bla

这一职能在投入类别中将具有反常性。 (然而,值得指出的是,职能不能算作准<>m>,也值得说的是,我必须有一个<代码>的例子。 Foo或Foo 试验,以便获得<代码>te方法的功能标语。 当然,这是第一次观察之后的。

一项功能是一个目标,不能凌驾于之上,因为这毫无意义。 方法可以压倒一切。 然而,正如我前面所说,在方法参数类型中,压倒性并非多变。 例如:

class A {
  def foo(a : Any) = println("A: " + a)
}

class B extends A {
  override def foo(s : String) = println("B " + s) //will not compile!
}

我的上述两种方法是两种不同的方法:动态发送只针对方法目标(即所呼吁的目标)。

例如,如果你删除<代码>overe的声明,则该守则将汇编成册。 如果是:

(new B).foo(1)   //prints A 1
(new B).foo("s") //prints B s

This is because, although both methods are called foo, they are completely different methods (i.e. I have overloaded foo, not overridden it). It s best understood as being that a method s arguments (incl their types) form part of that method s unique name. One method overrides another only if they have exactly the same name.


基本上,你将你提出的问题中的两点内容()混为一谈,我将澄清:

  • The variance annotations on Function1 define what it means for one function to be a subtype of another (and hence assignable to a reference of a given type).
  • Methods can be overridden on subclasses and the language specification outlines rules for when such overriding takes place.
问题回答

The relevant snippets of the spec:

<>Methodtypes

一种方法类型在内部称为(Ps)U,其中(Ps)为一系列参数名称和类型(p1 :T1,...pn :Tn),其中部分为n≥0U。 这种类型是指采用以下标语的标明方法:p1, ......, pn, 类型T1,..., Tn, 且其结果为。 U

Method types do not exist as types of values. If a method name is used as a value, its type is implicitly converted to a corresponding function type (§6.26).

<><>Overriding

成员<代码>M 类别C,matches<>em> (§ 5.1.3) a non- private member M' C据说优先于该成员。 在这种情形下,压倒性成员的约束<条码>。 M'。

<<>Conformance

如果Ti ′ Ti' for i = 1, ......, n and U 则该方法类型为(第1页:T1,...pn :Tn)U

某类复合类类别中的申报或定义C,在下述一种情况下,在某种复合类型或类别类别中添加相同名称的另一种声明:C>

  • A value declaration or definition that defines a name x with type T subsumes a value or method declaration that defines x with type T′, provided T <: T′.

你可以推翻并改变返回类型为亚类,但在接受超级类型以证明理由的同时,不能满足替代原则(正如 j一样)。 原因是,你也可以超负荷使用方法(使用相同名称、不同论点和类型等的手法),而你的手法也将被考虑过超负荷。 我猜测,这主要是一个核查和核查机制兼容问题,是一个合理的光谱。 超负荷已经使微粒光谱变得相当复杂。 简单地将压倒性的方法移至经修改的签字后超载的方法,可能很好:

class FooTest[A] extends Test[A] {
   override def test(a: Fasel) : Fasel = test(a.asInstanceOf[Bla])
   def test(a: Bla) : Fasel = new Fasel
}

你们可以做的是,只有反常(简单化,作为理由类型,而不是结果类型)才具备某种参数。

trait Test[-A] {
  // note the - before A. 
  // You might want to constraint with -A >: Fasel
  def tester(a: A) : Bla = new Bla
}

class FooTest extends Test[Bla] {
  override def tester(a: Bla): Fasel = new Fasel
}

val testOfBla: Test[Bla] = new FooTest
val testOfFasel: Test[Fasel] = testOfBla 
  // you can assign a Test[Bla] to a test[Fasel] because of the -A

在你的第二个例子中,tester(> in /code> 宣布Fasel ,但以>的压倒签名>>的,以宣布为<>。 Bla 作为论据。 由于<代码>Fasel是一个子类: Bla by their extends行各等级可能是错误的。





相关问题
Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

C++ Class Inheritance problem

Hi I have two classes, one called Instruction, one called LDI which inherits from instruction class. class Instruction{ protected: string name; int value; public: Instruction(string ...

Overloading a method in a subclass in C++

Suppose I have some code like this: class Base { public: virtual int Foo(int) = 0; }; class Derived : public Base { public: int Foo(int); virtual double Foo(double) = 0; }; ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

Extending Flex FileReference class to contain another property

I want to extend the FileReference class of Flex to contain a custom property. I want to do this because AS3 doesn t let me pass arguments to functions through event listeners, which makes me feel sad,...

Interface Inheritance in C++

I have the following class structure: class InterfaceA { virtual void methodA =0; } class ClassA : public InterfaceA { void methodA(); } class InterfaceB : public InterfaceA { virtual ...