Is there any way to extend an object?

前端 未结 6 851
礼貌的吻别
礼貌的吻别 2020-12-05 06:29

In scala, we cannot extend object:

object X 
object Y extends X

gives an error error: not found: type X

I

6条回答
  •  再見小時候
    2020-12-05 07:02

    You can convert parent into class + companion object, and then have child extend class E.g.

    in Parent.scala

    class Parent {}
    
    object Parent extends Parent {}
    

    And then in Child.scala

    object Child extends Parent {}
    

    Yes, it's more a hack than a solution.

提交回复
热议问题