我想在Scala中编写一个类,它采用任意数量的字节或像这样的布尔值
class Bytes(data: Byte*) {
def this(data: Boolean*) = this {
val res: Array[Byte] = convBools2Bytes(data)
res: _*
}
// […]
}
Run Code Online (Sandbox Code Playgroud)
其中convBools2Bytes是一个转换函数Array[Boolean]到Array[Byte]:
def convBools2Bytes(data: Array[Boolean]): Array[Byte]
Run Code Online (Sandbox Code Playgroud)
这给了我以下编译器错误:
[error] Bytes.scala:5: no `: _*' annotation allowed here
[error] (such annotations are only allowed in arguments to *-parameters)
[error] res: _*
[error] ^
Run Code Online (Sandbox Code Playgroud)
据我了解,该res: _*语句将Array[Byte]转换为重复参数列表(如"Scala编程"第2章第8节中所述).
为什么会出现这样的错误,我该如何避免呢?