Scala Function Variance and Overriding

后端 未结 4 628
野性不改
野性不改 2021-02-04 18:09

I\'m having a little problem understanding variance of methods when overloading.

While this perfectly works due to covariance in the return type

class Bl         


        
4条回答
  •  忘掉有多难
    2021-02-04 19:04

    The relevant snippets of the spec:

    Method Types

    A method type is denoted internally as (Ps)U , where (Ps) is a sequence of parameter names and types (p1 :T1,...,pn :Tn) for some n≥0 and U is a (value or method) type. This type represents named methods that take arguments named p1, ..., pn of types T1,...,Tn and that return a result of type 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

    A member M of class C that matches (§5.1.3) a non-private member M′ of a base class of C is said to override that member. In this case the binding of the overriding member M must subsume (§3.5.2) the binding of the overridden member M′.

    Conformance

    If Ti ≡ Ti′ for i = 1, ..., n and U conforms to U′ then the method type (p1 : T1,...,pn :Tn)U conforms to (p1′ :T1′,...,pn′ :Tn′)U′.

    Subsumes

    A declaration or definition in some compound type of class type C subsumes another declaration of the same name in some compound type or class type C′ , if one of the following holds.

    • 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′.

提交回复
热议问题