【问题标题】:Scala type alias with type parameters带有类型参数的 Scala 类型别名
【发布时间】:2014-10-06 13:07:03
【问题描述】:

让下面的类型别名

class Container[T]
type MyInt = Container[Int]

尝试过是否可以以及如何在类型别名中声明类型参数

type MyInt2 = Container[T <: Int]    // error: ']' expected but '<:' found.

【问题讨论】:

    标签: scala generics types alias


    【解决方案1】:

    你可以这样做:

    type MyInt2[T <: Int] = Container[T]
    

    对于其他成员(例如def),类型成员必须在其声明/签名(左)中声明类型参数,而不是在正文中(右)。

    【讨论】:

      猜你喜欢
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      • 2013-03-18
      • 2021-11-06
      • 2013-06-14
      • 1970-01-01
      相关资源
      最近更新 更多