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 ?
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
.
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