KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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(length) class BoatJourney(length: Int) extends Journey(length) class Port[J <: Journey] { def startJourney: J = { new J(23) // error: class type required but J found } } Is this even feasible? I'm familiar with Scala manifests but I'm not clear how they could help here. Likewise I can't figure out how to do the same with a companion object's apply() constructor: object Journey { def apply() = new Journey(0) } object PlaneJourney { def apply() = new PlaneJourney(0) } object BoatJourney { def apply() = new BoatJourney(0) } class Port[J <: Journey] { def startJourney: J = { J() // error: not found: value J } } Any thoughts gratefully received!
Tags (comma-separated)
Save Edits
Cancel