Subtype in Scala: what is “type X <: Y”?

后端 未结 2 1959
醉话见心
醉话见心 2020-12-31 17:05

Can anyone explain the subtype(<:) in the following code? Why it could be used like that? When we use that? Thanks.

trait SwingApi {

    type ValueChange         


        
2条回答
  •  时光取名叫无心
    2020-12-31 17:37

    By virtue of the abstract type members ValueChanged and ButtonClicked, trait SwingApi is itself uninstantiable (all traits are, but if they are fully implemented, they are trivially turned into a concrete class that can be instantiated).

    These constraints say that instantiable subtypes of SwingApi must define ValueChanged and ButtonClicked as subtypes of Event.

    The type aliases TextField and Button are constrained as structural types (they need not particular subclass relationship, but simply must supply the specified members with the specified types).

    The why of it is simply generality. This imposes the minimal constraints on the implementors of trait SwingApi necessary for it to be used by code that demands a SwingApi.

提交回复
热议问题