我想要寫Scala中的一個類,它的字節或布爾值的這樣一個重複參數
class Bytes(data: Byte*) {
def this(data: Boolean*) = this {
val res: Array[Byte] = convBools2Bytes(data)
res: _*
}
// […]
}
任意數,其中convBools2Bytes
是一種轉換Array[Boolean]
到Array[Byte]
的函數:
def convBools2Bytes(data: Array[Boolean]): Array[Byte]
這給了我以下編譯器錯誤:
[error] Bytes.scala:5: no `: _*' annotation allowed here
[error] (such annotations are only allowed in arguments to *-parameters)
[error] res: _*
[error] ^
據我所知,res: _*
語句將Array[Byte]
變成一個重複參數列表(如在「Programming in Scala」, 2nd Ed.的第8.8節中所述)。
爲什麼會出現這樣的錯誤,我該如何避免它?
我剛剛插入'convBools2Bytes'來表達清楚。沒想過。在真正的實現中,這個塊中有很多代碼。所以在這個例子中,它確實沒有任何意義。 – pvorb 2012-07-23 20:55:07