开发者

Scala - can unapply return varargs?

Object L1 below works. I can "create" an L1 by passing in varargs, which is nice, but I would like to be able to assign to an L1 using the same syntax. Unfortunately, the way开发者_JAVA技巧 I've done it here requires the uglier syntax of nesting an Array inside the L1.

object L1 {
    def apply(stuff: String*) = stuff.mkString(",")
    def unapply(s: String) = Some(s.split(","))
}
val x1 = L1("1", "2", "3")
val L1(Array(a, b, c)) = x1
println("a=%s, b=%s, c=%s".format(a,b,c))

I attempted accomplish this in what seems like an obvious way, as in L2 below:

object L2 {
    def apply(stuff: String*) = stuff.mkString(",")
    def unapply(s: String) = Some(s.split(","):_*)
}
val x2 = L2("4", "5", "6")
val L2(d,e,f) = x2
println("d=%s, e=%s, f=%s".format(d,e,f))

But this give the error:

error: no `: _*' annotation allowed here 
(such annotations are only allowed in arguments to *-parameters)`.

Is it possible for unapply to use varargs in this way?


I think what you want is unapplySeq. Jesse Eichar has a nice write up on unapplySeq

scala> object L2 {
     |     def unapplySeq(s: String) : Option[List[String]] = Some(s.split(",").toList)
     |     def apply(stuff: String*) = stuff.mkString(",")
     | }
defined module L2

scala> val x2 = L2("4", "5", "6")
x2: String = 4,5,6

scala> val L2(d,e,f) = x2
d: String = 4
e: String = 5
f: String = 6
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜