New to Scala. The language is quite concise.
Curious why implementing multiple traits requires multiple "with" statements.
For example:
class Foo extends Bar with A with B with C {}
vs.
class Foo extends Bar with A, B, C {}
We can extend from multiple traits, but only one abstract class. Abstract classes can have constructor parameters, whereas traits cannot.
It means an abstract type member is defined (inside some context, e.g. a trait or class), so that concrete implementations of that context must define that type.
While we cannot extend multiple abstract classes, we can extend multiple traits in Scala.
In scala, trait is a collection of abstract and non-abstract methods. You can create trait that can have all abstract methods or some abstract and some non-abstract methods. A variable that is declared either by using val or var keyword in a trait get internally implemented in the class that implements the trait.
Consider this
class Foo[A, B]
new Foo[Bar with D with E, Bar with E] {}
vs
new Foo[Bar with D, E, Bar with E] {}
It just isn’t unambiguous in all cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With