Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

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] ?

like image 475
i.am.michiel Avatar asked Jul 29 '12 09:07

i.am.michiel


People also ask

What is Option string?

The STRING option in GET and PUT statements transmits data between main storage locations rather than between the main and a data set. DBCS data items cannot be used with the STRING option.

What is Option [] in scala?

The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null.

How do I create an option in scala?

An Option[T] can be either Some[T] or None object, which represents a missing value. For instance, the get method of Scala's Map produces Some(value) if a value corresponding to a given key has been found, or None if the given key is not defined in the Map.

What is getOrElse in scala?

As we know getOrElse method is the member function of Option class in scala. This method is used to return an optional value. This option can contain two objects first is Some and another one is None in scala. Some class represent some value and None is represent a not defined value.


1 Answers

You can also just use Option(myValue) which will convert null to None and non-null to Some.

like image 119
Channing Walton Avatar answered Sep 23 '22 15:09

Channing Walton