How can I invoke the constructor of a Scala abstract type?

后端 未结 3 762
攒了一身酷
攒了一身酷 2021-02-14 17:11

I\'m trying to figure out how to invoke a constructor for a Scala abstract type:

class Journey(val length: Int)
class PlaneJourney(length: Int) extends Journey(l         


        
3条回答
  •  青春惊慌失措
    2021-02-14 17:39

    My inclination is that this cannot be done. I am far from a Scala guru, but my reasoning is this:

    1. You have a class Port with a type argument T where T must inherit from Journey (but T does not have to be exactly Journey, this is important).
    2. Within Port, you define a method which creates a new T. This class has no idea what T is, and therefore what T's constructor looks like.
    3. Because you don't know what arguments T's constructor takes, you don't know what arguments to pass to it.

    The solutions to this problem are handled very nicely in another question, so I will point you there for them rather than repeating here: Abstract Types / Type Parameters in Scala

提交回复
热议问题