What does [B >: A] do in Scala?

前端 未结 3 1832
轮回少年
轮回少年 2021-02-01 02:18

What does [B >: A] mean in Scala? And what are the effects?

Example reference: http://www.scala-lang.org/node/129

class Stack[+A] {
             


        
3条回答
  •  無奈伤痛
    2021-02-01 03:01

    [B >: A] is a lower type bound. It means that B is constrained to be a supertype of A.

    Similarly [B <: A] is an upper type bound, meaning that B is constrained to be a subtype of A.

    In the example you've shown, you're allowed to push an element of type B onto a stack containing A elements, but the result is a stack of B elements.

    The page where you saw this actually has a link to another page about lower type bounds, which includes an example showing the effect.

提交回复
热议问题