Scala 什么时候不能指定类型参数

Scala 什么时候不能指定类型参数,scala,Scala,给定以下测试用例,其中有3个场景。我想问一下,这里有什么规则可以控制何时必须指定类型参数,何时不必指定类型参数 @Test def testTypeParamter(): Unit = { class Cat[A] //1. Don't need to specify the type parameter for Cat def getCat() = new Cat println(getCat()) import scala.collect

给定以下测试用例,其中有3个场景。我想问一下,这里有什么规则可以控制何时必须指定类型参数,何时不必指定类型参数

  @Test
  def testTypeParamter(): Unit = {

    class Cat[A]

    //1. Don't need to specify the type parameter for Cat
    def getCat() = new Cat
    println(getCat())

    import scala.collection.mutable.ArrayBuffer

    //2. Don't need to specify the type parameter for ArrayBuffer

    val bf = new ArrayBuffer()
    println(bf)

    //3. Do need to specify the type parameter for ArrayBuffer to make bf2 += 1 work
    val bf2 = new ArrayBuffer[Int]()
    bf2 += 1

    println(getCat())
  }
与#2和#3相比,如果我们创建一个没有类型参数的空ArrayBuffer,我们该怎么办呢

  @Test
  def testTypeParamter(): Unit = {

    class Cat[A]

    //1. Don't need to specify the type parameter for Cat
    def getCat() = new Cat
    println(getCat())

    import scala.collection.mutable.ArrayBuffer

    //2. Don't need to specify the type parameter for ArrayBuffer

    val bf = new ArrayBuffer()
    println(bf)

    //3. Do need to specify the type parameter for ArrayBuffer to make bf2 += 1 work
    val bf2 = new ArrayBuffer[Int]()
    bf2 += 1

    println(getCat())
  }
1.1没有类型参数的场景

scala> class Bag[A]
defined class Bag

scala> def createBag = new Bag
createBag: Bag[Nothing]
scala>  val buffer = new ArrayBuffer()
buffer: scala.collection.mutable.ArrayBuffer[Nothing] = ArrayBuffer()
1.2无类型参数的ArrayBuffer[T]

scala> class Bag[A]
defined class Bag

scala> def createBag = new Bag
createBag: Bag[Nothing]
scala>  val buffer = new ArrayBuffer()
buffer: scala.collection.mutable.ArrayBuffer[Nothing] = ArrayBuffer()
在这两种情况下,默认类型参数都是
scala.Nothing
<代码>scala。没有任何东西是抽象的,不能实例化,这意味着您不能像
buffer+=新字符串(“apple”)
等,因为
的底部没有任何内容

2。提供类型参数

显然,这就是泛型类型的目的,您希望泛型用于特定类型

scala> var buffer = new ArrayBuffer[Long]()
buffer: scala.collection.mutable.ArrayBuffer[Long] = ArrayBuffer()

scala> buffer+=89l
res0: scala.collection.mutable.ArrayBuffer[Long] = ArrayBuffer(89)
让我们签入REPL

1.1没有类型参数的场景

scala> class Bag[A]
defined class Bag

scala> def createBag = new Bag
createBag: Bag[Nothing]
scala>  val buffer = new ArrayBuffer()
buffer: scala.collection.mutable.ArrayBuffer[Nothing] = ArrayBuffer()
1.2无类型参数的ArrayBuffer[T]

scala> class Bag[A]
defined class Bag

scala> def createBag = new Bag
createBag: Bag[Nothing]
scala>  val buffer = new ArrayBuffer()
buffer: scala.collection.mutable.ArrayBuffer[Nothing] = ArrayBuffer()
在这两种情况下,默认类型参数都是
scala.Nothing
<代码>scala。没有任何东西是抽象的,不能实例化,这意味着您不能像
buffer+=新字符串(“apple”)
等,因为
的底部没有任何内容

2。提供类型参数

显然,这就是泛型类型的目的,您希望泛型用于特定类型

scala> var buffer = new ArrayBuffer[Long]()
buffer: scala.collection.mutable.ArrayBuffer[Long] = ArrayBuffer()

scala> buffer+=89l
res0: scala.collection.mutable.ArrayBuffer[Long] = ArrayBuffer(89)

当Scala推断出所需的类型时,不需要指定类型参数。此推断基于方法/构造函数的参数(在您的案例中没有)和预期类型(在您的案例中没有)。你也可以有

val buffer: ArrayBuffer[Long] = new ArrayBuffer()

当Scala推断出所需的类型时,不需要指定类型参数。此推断基于方法/构造函数的参数(在您的案例中没有)和预期类型(在您的案例中没有)。你也可以有

val buffer: ArrayBuffer[Long] = new ArrayBuffer()

感谢@prayagupd提供了有用的答案。REPL是一个很好的工具,但我忘了。是的,scala REPL或clojure REPL是快速试用的非常重要的工具。关于何时可以使用
泛型[Nothing]
,我不认为您会想使用它,而是使用
选项[Generics[t]]
。顺便说一下,我们已经扩展了
选项[Nothing]
。谢谢@prayagupd提供的有用答案。REPL是一个很好的工具,但我忘了。是的,scala REPL或clojure REPL是快速试用的非常重要的工具。关于何时可以使用
泛型[Nothing]
,我不认为您会想使用它,而是使用
选项[Generics[t]]
。顺便说一下,我们已经扩展了
选项[Nothing]