简体   繁体   中英

Scala Seq case match

I have a Sequence that I would like to case match. The question that I have is quite simple:

Are the following two cases the same?

case x :: Nil =>
case x :: xs  =>

Can I instead just have

case Nil =>
case x :: xs  =>

How would the case x :: Nil be handled? Would it also match the case Nil?

No, they are not the same.

case x :: Nil matches a list with exactly one element.

case x :: xs matches a list that has at least one element.

case Nil matches an empty list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM