Are we really “expanding” in scala?

During the training of Scala, I combed my hair with an understanding of a specific nuance:

trait Test

class Tester with Test //#A: Throws compilation error

class Tester extends Test //#B: works just fine

Now we really are not expanding the trait as such (or are we?). Here are my questions:

  • How is a feature extension different from a class extension? Is this just a special syntax for one attribute, and I just accept it as a language nuance?
  • Why is #A wrong? Why can't we do this? Why should we expand the line if there is no superclass for inheritance?
Martin Odersky said in response to a similar question that it was a convention, but people found it confusing. I am not sure why this would be so, or what problems led to this solution? But here is his answer:

We had this agreement at an early stage in Scala design. People have found this confusing. That's why we changed the use of always `extends'. As I see like this:

class A extends B with C {...}

should be decomposed as:

class A extends <<B with C {...} →>

That is, A is a class that extends the anonymous pattern of B with C {...}. It doesn't matter if this template starts with a class or a dash.

Another advantage of the new convention is that subclasses are not if the class is changed to hell or vice versa, something happens quite often and naturally.

Although I can live with his explanations and retrain my thinking (and see the advantage of changing the class to hell, a simple side effect of design choice), I would still like to understand this nuance more intuitively.

+4
1

, , "" "" trait ,

, Structural Type Self Referencing Trait,

type Fooable = {
  def foo(): Unit
}


trait FooableExtra { self: Fooable =>

  def omgWeCanFoo(): Unit = {
    println("foo :: by :: FooableExtra")
    self.foo()
  }

}

class OneThingWithFoo extends FooableExtra {

  def foo(): Unit = {
    println("foo :: by :: OneThingWithFoo")
  }

  def oneThing: Unit = {}
}

... , , - class A extends TraitA.

+1

Source: https://habr.com/ru/post/1670127/


All Articles