سؤال

Here recentest is a list and I want to match on its "profile": either empty or exactly one element. Can I do it natively in a match statement?

val newId = if( recentest.size == 0) 0L
    else {recentest(0).as[Long]("item_id") + 1}
هل كانت مفيدة؟

المحلول 2

val newId = recentest match {
   case Nil    => 0
   case h::Nil => h.as[Long]("item_id") + 1
}

نصائح أخرى

If you want to match an arbitrary size among several cases you could do this:

list match {
  ...
  case _ if list.length == mySize => ...
  ...
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top