Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala. Partial classes

Tags:

scala

Is there any equivalents to C# partial classes in scala ? I would like to depart my functionality with objects like this:

// 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 achive this functionality with scala ?

like image 898
Daryl Avatar asked Feb 10 '23 02:02

Daryl


1 Answers

The closest you can do is to divide your functionality into traits, and divide those across 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 the self-type and setting it to MainClass, you ensure that each trait can reference all the members that will eventually be mixed into the MainClass.

like image 86
axel22 Avatar answered Feb 16 '23 04:02

axel22