Why do I get the error below? How to workaround it?
I assumed that since A and B compile to (interface,class) pairs, it's a matter of choosing the right static method call to implement when compiling C. I would expect the priority to be according to order.
scala> trait A { def hi = println("A") } defined trait A scala> trait B { def hi = println("B") } defined trait B scala> class C extends B with A <console>:6: error: error overriding method hi in trait B of type => Unit; method hi in trait A of type => Unit needs `override' modifier class C extends B with A scala> trait A { override def hi = println("A") } <console>:4: error: method hi overrides nothing trait A {override def hi = println("A")}
Note that in Ruby this works well:
>> module B; def hi; puts 'B'; end; end => nil >> module A; def hi; puts 'A'; end; end => nil >> class C; include A; include B; end => C >> c = C.new => #<C:0xb7c51068> >> c.hi B => nil
Classes and objects can extend traits, but traits cannot be instantiated and therefore have no parameters.
Unlike a class, what's important to remember, is that you cannot add extends or implements to a trait. You can't instantiate a trait, either. Their sole purpose is to support our classes, and not to replace them. Traits can have methods, just like classes do.
A class can extend only one abstract class, but it can implement multiple traits, so using traits is more flexible.
Unlike the other types, however, traits cannot be instantiated. Traits look about the same as any other type of class. However, like objects, they cannot take class parameters.
This works for me in 2.8 and 2.11, and would allow you to be non-intrusive in traits A
or B
:
trait A { def hi = println("A") } trait B { def hi = println("B") } class C extends A with B { override def hi = super[B].hi def howdy = super[A].hi // if you still want A#hi available } object App extends Application { (new C).hi // prints "B" }
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