I have 3 functions, kk expect Array[Byte]
or List[Array[Byte]]
, So I did a pattern matching,
def gg (a :Array[Byte]) = "w"
def ff (a :List[Array[Byte]]) = "f"
def kk(datum : Any) = datum match {
case w : Array[Byte] => gg(w)
case f :List[Array[Byte]] => ff(f)
case _ => throw new Exception("bad data")
}
and I get an error when I try to compile the code:
non-variable type argument List[Any] in type pattern List[List[Any]] (the underlying of List[List[Any]]) is unchecked since it is eliminated by erasure
so instead I construct my kk
function as flowing and it can be compiled now:
def kk(datum : Any) = datum match {
case w : Array[Byte] => gg(w)
case f :List[_] => ff(f.asInstanceOf[List[Array[Byte]]])
case _ => throw new Exception("bad data")}
My questions:
1: is my current version of kk
is idiomatic way to do a pattern matching for List, if not can someone show to how to do it?
2: say if I want to pattern matching List[Any]
and List[List[Any]]
, how am able to do that? (ff(f.asInstanceOf[List[Array[Byte]]])
may cause an error if I datum is type of List[Byte]
)