![]() Types! Anyway, I am not alone in finding this weird,Īpparently. I found thisĮspecially bizarre because you are explicitly calling. ![]() An Option T can be either Some T or None object, which represents a missing value. Have to project it to a RightProjection (chosing Right vs Left is simplyĬonvention) since Either itself doesn’t have a flatMap method. Scala Option T is a container for zero or one element of a given type. def toRight ( x : Option, orElse : => Err ) : Either.RightProjection = // prints "Error, msg was 'z was None'" in this case.Ĭonfusingly to me, after converting the Option to a Right or Left you Since we want to add new functionality to an existing type without being able to modify the source code, the way to go are implicits.Val x : Option = Some ( 1 ) val y : Option = Some ( 2 ) val z : Option = None // Just look away from that return type. So, since it’s not there (yet) we have to come up with a solution ourselves. Personally, I would find such an API for Option very intuitive to use, concise, compact and fully OO. ![]() ![]() Val converted = myOpt some_? (_.("-")) orNone "unknown" By passing the toInt method into the map method, you can convert every element in the collection into a Some or None value: scala> bag. Val converted = if(myOpt.isDefined) .mkString("-") else "unknown"Īpparently, it’s not that hard to put the desired conversion in one line of code, however, using conditional logic to achieve the desired result tempers my enthusiasm considerably. By using other methods of the Option API I could convert this into a one-liner as follows: The good news is, that there is an alternative. #Scala option codeFour lines of code to perform a simple conversion or provide a default value depending whether we are dealing with a Some or a None is quite a lot. This is the only case I can think of where I can associate Scala with boilerplate code. Otherwise we want to provide a default value of “unkown”. Use Option(possiblyNull) to avoid instances of Some(null). To illustrate this let’s consider the following snippet:Ĭase Some(v) => v.("-")īasically, what we want to achieve here is a conversion from Int to a String depending whether the Option is of type Some(Int). Error handling strategies in functional Scala Use Option to return optional values. When writing Scala code I find myself using this construct quite often. Otherwise, in case it’s a None, I want to provide a default value of the target type Y. Whereas a programmer can express his/her intent in Scala in an extremely concise and compact way I must admit that this is not always true when dealing with Options.ĭepending whether I’m dealing with a Some() a want to convert the value of type X to a value of another type Y. Options provide a very concise way to express whether a variable can have a value or not. Read on to see how easy it is to tailor any kind of existing Scala type to perfectly fit your needs based on an example with Options. The scala package contains core types like Int, Float, Array or Option which are accessible in all Scala compilation units without explicit qualification or imports. ![]() #Scala option how toThis blog explains how to deal with this particular case in an elegant way using implicits. This is the documentation for the Scala standard library. However, it’s usage in certain cases can lead to rather verbose code. Most libraries and applications make use of this handy type. The Scala Option type is key for dealing with variables that can have values or not. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |