Suppose I have the following trait
trait Market {
def getCurrency: String
}
With the following implementation
class MarketImpl extends Market {
override def getCurrency = "USD"
}
I have another abstract class as follows
abstract class TraitTest extends Market {
}
What is the syntax for instantiating TraitTest using the MarketImpl implementation? Conceptually something like the following
new TraitTest with MarketImpl
Although the above does not work because MarketImpl is not a trait
You’ve hit the single class inheritance limit of Scala (and Java). You can fix it using instance composition/aggregation (in the same way you would using Java):
and then you instantiate: