Is there any way to extend an object?
As so often the correct answer depends on the actual business requirement. Extending from an object would in some sense defy the purpose of that object since it wouldn't be a singleton any longer.
What might be a solution is to extract the behavior into an abstract trait. And create objects extending that trait like so:
trait T{
// some behavior goes here
}
object X extends T
object Y extends T {
// additional stuff here
}
If you want use methods and values from another object you can use import.
object X{
def x = 5
}
object Y{
import X._
val y = x
}
You can't actually extend an object, because that would create two of it, and an object by definition exists only once (edit: well, that's not quite true, because the object definition can be in a class or method).
For your purposes, try this:
object X {
}
object Y {
def a = 5
}
implicit def xToY(x: X.type) = Y
println(X.a)
It doesn't actually extend, but it does allow you to call new methods on it than were originally defined.