I am playing around with Scala at the moment and the pattern matching. I have the general idea behind it and can get the basics working. My issue is with Option[]. It is possible to use pattern matching on Option[]'s?
What I am trying to do is make a little function that will take in an option[String] parameter and then based on the input return the string if its a string and a heads up if not. I amnt too sure on how to go about this though, I have tried a few thing but it either gives out or in the case below will never hit the second case.
def getString(someString: Option[String]): String =
someString match {
case s: Option[String] => someString //also tried things like case: String => ...
case _ => s"no string entered" //and things like case _ => ...
}
case Some(someString) => someString- The fourth birdcase s:Stringthen as it is still passing in a string? - RJHazeOption[T]is an ADT it is eitherSome(value: T)orNone. So when you pattern match you may check for those two possibilities. - Alsocase x: Cwhat that does is checking ifxis an instance of the classC, so if you already know thatsis anOptionthen it doesn't make sense to test if it is an instance ofString. - Luis Miguel Mejía Suárez