How to extend an object in Scala with an abstract class that has a constructor, and apply method of the object returns the object as subtype of the abstract?
for example :
abstract class AbstractResource(amount:Int) {
val amount:Int
def getAmount = amount
}
case object Wood extends AbstractResource{
def apply(amount: Int) = {
// something that returns the subtype
}
}
I think a good solution is:
abstract class AbstractResource {
val amount: Int = 0
def getAmount = amount
}
case object Wood extends AbstractResource {
def apply(quantity: Int) = {
new AbstractResource {
override val amount = quantity
}
}
}
But my problem is I can't edit AbstractResource
case class Wood(amount: Int) extends AbstractResource(amount)?Woodhave to extendAbstractResource? Note that the thingWood#applyis returning is not of typeWood.Woodmust call the constructor ofAbstractResource. I'm not sure what you're trying to achieve.