在 REPL 中有效但在 Scala 中无效的类型别名 class/object

Type aliases that work in the REPL but not in a scala class/object

我有一个测试程序,其中有内存静态数组。为了简洁起见,我使用了类型别名。

REPL 中的以下作品

type >[T] = Array[T]
val dat = >(>(1,2,3),>(2,3,4))

dat: Array[Array[Int]] = Array(Array(1, 2, 3), Array(2, 3, 4))

然而,将标识符从“>”更改为 "A" 确实 无效 :类型已创建,但与上面使用的创建数组的相同语法失败:

scala> type A[T] = Array[T]
defined type alias A

scala> val dat = A(A(1,2,3),A(2,3,4))
<console>:7: error: not found: value A
       val dat = A(A(1,2,3),A(2,3,4))

此外,以上两个都不能在 Scala 程序中工作 AFAICT:

  test("VectorProjection") {
    type  A[T] = Array[T]
    // Next line shows RED for all the A's and also has compiler error: "not found: value A"
    val dat  = A(A(1., 2., 3.), A(1.5,2.,2.5), A(2.,3.8,5.6), A(2.5,3.0,3.5), A(3.1,3.7,4.3) )
    val firsteigen =  subtractProject(dat(0), dat(4))
  }

寻找:

更新 根据 James Iry 的建议,以下方法确实有效:

    def A[T : ClassTag](ts: T*) = Array(ts:_*)

这是实际操作:

  test("VectorProjection") {
    def A[T : ClassTag](ts: T*) = Array(ts:_*)
    val dat  = A(
      A(1., 2., 3.),
      A(1.5,2.,2.5),
      A(3.,6.,9.)  )
    val firstEigen =  subtractProject(dat(0), dat(5))
    println(s"firstEigen: ${firstEigen.mkString(",")}")
  }

另一个更新另一个答案更接近这个 OP:

一起使用 type 和 val:

    type A = Array[Double]
    val A = Array

这是实际操作:

  test("VectorProjection") {
    type A = Array[Double]
    val A = Array
    val dat  = A(
      A(1., 2., 3.),
      A(1.5,2.,2.5),
      A(3.,6.,9.)  )
    val firstEigen =  subtractProject(dat(0), dat(5))
    println(s"firstEigen: ${firstEigen.mkString(",")}")
  }

我无法通过“>”复制您的成功

scala> type >[T]=Array[T]
defined type alias $greater

scala> >(1,2,3)
<console>:8: error: not found: value >
              >(1,2,3)
              ^

至少,在我定义它之前不会

scala> import scala.reflect._
import scala.reflect._

scala> def >[T : ClassTag](ts: T*) = Array(ts:_*)
$greater: [T](ts: T*)(implicit evidence: scala.reflect.ClassTag[T])Array[T]

scala> >(1,2,3)
res1: Array[Int] = Array(1, 2, 3)

同样适用于 A

scala> type A[T]=Array[T]
defined type alias A

scala> A(1,2,3)
<console>:11: error: not found: value A
              A(1,2,3)
              ^

scala> def A[T : ClassTag](ts: T*) = Array(ts:_*)
A: [T](ts: T*)(implicit evidence: scala.reflect.ClassTag[T])Array[T]

scala> A(1,2,3)
res2: Array[Int] = Array(1, 2, 3)

解释一下:类型 X = Y 只是为类型 X 创建同义词。它不会为可能与该类型相关联的所有其他内容(如伴生对象、构造方法等)引入同义词。

如果您创建值别名,它将起作用:

type A[T] = Array[T]
val A = Array
val dat = A(A(1,2,3),A(2,3,4)) //dat: Array[Array[Int]] = Array(Array(1, 2, 3), Array(2, 3, 4))

第 2 行创建一个值别名,因此您可以使用 A 类型别名创建值。它将依次调用 A.apply(1,2,3).

用这个来显示 repl 知道什么:

scala> $intp.definedTerms
res0: List[$intp.global.TermName] = List($intp)

scala> $intp.definedTypes
res1: List[$intp.global.TypeName] = List($greater)

例如,您可能有:

scala> object X
defined object X

scala> trait X
defined trait X
warning: previously defined object X is not a companion to trait X.
Companions must be defined together; you may wish to use :paste mode for this.

scala> type X = String
defined type alias X

但它不会对别名发出警告。