Why doesn't Scala fully infer type parameters when type parameters are nested?

后端 未结 3 1818
傲寒
傲寒 2020-12-31 08:46

Consider the following Scala code:

abstract class A
abstract class B[T <: A]
class ConcreteA extends A
class ConcreteB extends B[ConcreteA]

class Example         


        
3条回答
  •  不思量自难忘°
    2020-12-31 09:11

    I have composed a document of type inference workarounds on GitHub for my own learning.

    A few simple rules that I find useful are:

    • Type parameters of type parameters cannot be inferred: Scala type inference only sees types specified in the parameter list (not to be confused with type parameter list).

    • Previous parameters are not used to infer future parameters: Type information only flows across parameter lists, not parameters.


    However, in this particular example type members are the way forward (thanks @Kipton Barros!)

提交回复
热议问题