0

I'm trying to match case against a String and have the following code:

val selectedAnswers: List[Int] = questionType match {
  case "CHECK_BOX"    => {
    answerCheckBox match {
      case Some(answers) => answers
      case None => List()
    }
  }
  case "RADIO_BUTTON" => {
    answerRadio match {
      case Some(answer) => List(answer)
      case None => List()
    }
  }
  case _ => {
    List()
  }
}

Why is it not falling through the _ case when the String is not RADIO_BUTTON or CHECK_BOX?

The values for answerRadio and answerCheckbox are actually coming from the form that I'm submitting to the controller.

val (currentQuesId, questionType, answerRadio, answerCheckBox) = runExamForm.bindFromRequest.get

And the form declaration looks like:

  val runExamForm = Form(
    tuple(
      "currentQuestionId" -> number,
      "questionType" -> text,
      "answerRadio" -> optional(number),
      "answerCheckbox" -> optional(list(number))
    )
  )
7
  • Your code looks okay to me. If questionType is not "CHECK_BOX" or "RADIO_BUTTON" then you will end up with an empty List. Commented Dec 30, 2013 at 19:03
  • But when I try with a String like _BOX, it is not falling through and going to the case _ block but rather it throws an error. Commented Dec 30, 2013 at 19:06
  • Can you share a little more of your code? I made guesses about the types of answerCheckBox and answerRadio and questionType but it would be better if you showed what they were. Also, me testing with "_BOX" works okay; no errors. I'm using Scala 2.10 Commented Dec 30, 2013 at 19:10
  • What error does it throw when you use _BOX ? Commented Dec 30, 2013 at 19:11
  • Here is the error that I get: [MatchError: _BOX (of class java.lang.String)] Commented Dec 30, 2013 at 19:13

2 Answers 2

4

This is "equivalent" version of your code:

val selectedAnswers: List[Int] = questionType match {
  case "CHECK_BOX"    => answerCheckBox.toList.flatten
  case "RADIO_BUTTON" => answerRadio.toList
  case _ => List()
}

Does it work as expected?

Sign up to request clarification or add additional context in comments.

5 Comments

If you see my original post above, answerCheckBox and asnwerRadio are optional types, I need to have an inner case block to fetch them or return an empty List.
So if it's an Option then it should have toList method. Please check if it works
I think a few clarifications would help regarding your question: 1) scala version, 2) types of all variables in this code snippet, 3) complete stack trace of an exception
Note that calling .toList on an Option will give you a List of either 0 or 1 elements. This code is directly equivalent to the original posting. The only change I would make is to use Nil in place of List()
I was suspecting that it's some weird type that implements unapply incorrectly but I can't even convince myself of that :). I don't understand the secrecy behind the types involved. Did it finally work?! :)
0

It's a long shot, but try replacing the _ with some other name (x is fine) and make sure that your code contains nothing but regular whitespace.

Very rarely I've seen bizarre errors like this caused by other non-printing characters in the code, which always seemed to be caused by pasting code from a chat on the OSX Skype client.

Also... Can you confirm in your code sample which line the MatchError occurs on?

3 Comments

If you look at the first original post and follow the source code, the error ouucrs in the line "case "CHECK_BOX" => {"
and you can confirm that the code operates as expected if you pass in "CHECK_BOX" or "RADIO_BUTTON"?
Yes. It works perfectly fine when I pass in CHECK_BOX or RADIO_BUTTON.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.