Scala. Partial classes

Are there any equivalents to partial c # classes in scala? I would like to leave my functionality with such objects:

// file 1:
object MainClass {
    def addValue(value: AnyRef) = ???
}

// file 2:
partial object MainClass {
    addValue(1)
}

// file 3:
partial object MainClass {
    addValue(2)
}  

// file 4: initialize MainClass
MainClass.init()

How can I execute this function with scala?

+4
source share
1 answer

The closest thing you can do is split your functionality into traits and split them into separate files:

// file 1:
class MainClass extends Functionality1, Functionality2

// file 2:
trait Functionality1 {
  self: MainClass =>
}

// file 3:
trait Functionality2 {
  self: MainClass =>
}

Note that by using self-type and setting it to MainClass, you guarantee that every attribute can refer to all elements that will eventually be mixed with MainClass.

+7
source

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


All Articles