I am confused. In my existing project, I am not able to find the difference between extends
and with
. Could you please help me?
The first thing you inherit from can either be a trait or a class, using the extends
keyword.
trait SomeTrait
class SomeClass
class ThisIsValid extends SomeTrait
class ThisAsWell extends SomeClass
You can define further inherited traits (and only traits) using the with
keyword.
class AlsoThisWorks extends SomeClass with SomeTrait
If a trait inherits from a class you cannot use it like in the above example.
That's it regarding the extends
and with
keywords. If you want to learn more about classes and traits the official documentation goes in depth on the topic.