How to set a String in a Option[String]?

Viewed 66789

When I'm trying to affect a value of type String in a field of type Option[String] I get the following error :

type mismatch; found : String required: Option[String]

How can I affect value myValue:String into field myField:Option[String] ?

4 Answers

If you want to convert empty string to None more universal solution is: Option(str).filter(_.nonEmpty)

Related