1
class New {

  val x : Option[String] = "abc"
  val y : String = "abc"

  if(x == y) "YES" else "No"

}

  **Error:(5, 28) type mismatch;
  found   : String("abc")
  required: Option[String]
  val x : Option[String] = "abc"**

I am facing above type mismatch error. Can someone help to resolve above error?

2
  • 1
    Considering that you are facing such issues, I will advise you to learn Scala in a more structured manner. I suggest reading the book Essential Scala. You can download this for free on - underscore.io/training/courses/essential-scala Commented Apr 15, 2020 at 6:15
  • your problem is you trying assign String value to x but declare x as Option[String] this is different types. try to wrap "abc": Option("abc") and after that you will need to wrap y also for comparing. Read more about option for using it correctly. Commented Apr 15, 2020 at 7:51

1 Answer 1

2

The type mismatch is because String and Option[String] are different types and you can't directly compare them.

You probably want this:

if (x.contains(y)) "YES" else "No"

This checks whether x has something in it (is not None) and, if so, whether that something equals y.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.