Questions tagged [scala-option]

Option in the Scala language is a container for zero or one element of a given type.

Option in is a container for zero or one element of a given type.

Related tags

232 questions
0
votes
1 answer

How would call a function that calls "by-name" as an argument in scala

I'm pretty new to scala and still in the early days of learning. I was reading an article that had an example like so: def example(_list: List[Positions], function: Position => Option[Path]): Option[Path] = _list match {...} NB Position is a…
0
votes
2 answers

How to open an Option[Map(A,B)] in Scala?

I've done enough Scala to know what ugly code looks like. Observe: val sm Option[Map[String,String]] = Some(Map("Foo" -> "won", "Bar" -> "too", "Baz" -> "tree")) Expected output: : String = Foo=won,Bar=too,Baz=tree Here's my Tyler Perry code…
dlite922
  • 1,924
  • 3
  • 24
  • 60
0
votes
1 answer

Define Map with generic datatype value in Scala

I have to construct the Map in Scala so that I can collect all the data from call to Java code. Possible values are String, Integer, Double and null. Is there a way to represent this Map in Scala? I am trying to have Option as follow but not sure…
αƞjiβ
  • 3,056
  • 14
  • 58
  • 95
0
votes
1 answer

How Assign Value Option[List[String]] on Scala

This question is about assign parameter value Option[List[String]] in Scala. My code: def mapListString(pList : Option[List[String]]) = pList match { case None => "-" case Some(cocok) => cocok case _ => "?" }
0
votes
1 answer

Instantiate a case class with Options and Scala Enumeration in Java

It seems like if a case class has both enums and options, I cannot instantiate it from Java. Consider the following in Scala: object WeekDay extends Enumeration { type WeekDay = Value val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value } case…
Mahdi
  • 1,778
  • 1
  • 21
  • 35
0
votes
1 answer

Behavior of map() on Options

Im trying to map a JSONObject instance into an actual instance through Play Combinators. I am able to get the desrialization work correctly. The question is on the behavior of how map() works on an Option[JSONObject]. Option1: jsonVal:…
broun
  • 2,483
  • 5
  • 40
  • 55
0
votes
2 answers

How to handle `null` in Scala

When Scala app interacting with Java code it sometimes need to handle null returned by Java method. Which of this 2 ways is more scala idiomatic? Which one should I use? 1. val a = javaClass.javaMethod if (a == null) throw new…
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
0
votes
3 answers

Scala how to instantiate new Option[java.sql.Date] type

I have var date_of_birth2 : Option[java.sql.Date] = None. Later I have to fill date_of_birth2 by string. String date = "01/01/1990" var formate = new SimpleDateFormat("dd/MM/yyyy").parse(date) var aDate = new java.sql.Date(formate.getTime());…
masiboo
  • 4,537
  • 9
  • 75
  • 136
0
votes
3 answers

Scala default TypeTag

I'm using typetags in Scala to match options def foo[T: TypeTag](bar: Any) = { bar match { case option: Option[T] => option match { case x if typeOf[T] <:< typeOf[List[Any]] => println("List Option") case x if typeOf[T] <:<…
Ben
  • 696
  • 9
  • 19
0
votes
1 answer

DRY use of multiple fields of an Option's value

Background I'm using a java web UI library (Vaadin, but that's irrelevant - I'm not using the Scaladin add-on) which has a TextField that has a method setValue(String) to set the current text value. As it's a java library, it has no notion of Option…
herman
  • 11,740
  • 5
  • 47
  • 58
0
votes
1 answer

How to implement flatMap for Option

I'm trying to implement map and flatMap as an extension/enrichment for Option, without cheating and looking at how it was implemented in Scalaz. So here's what I got so far before I got stuck: package extensions.monad trait Monad[M[_]] { // >>=…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
0
votes
2 answers

Scala Sugar for Options of Seq

I need to map a sequence, and if empty consider it a None rather than a empty sequence. So : val quote_ = quote_inner_element.map( q=> { val quote_link_regex(quote_link_id) = q.attr("href") Quote(quote_link_id.toInt) }) val quote =…
Hassan Syed
  • 20,075
  • 11
  • 87
  • 171
0
votes
4 answers

using if instead of match on Option[X]

I want to replace the below match with an if statement, preferably less verbose than this. I personally find if's to be easier to parse in the mind. val obj = referencedCollection match{ case None => $set(nextColumn ->…
Jesvin Jose
  • 22,498
  • 32
  • 109
  • 202
0
votes
2 answers

Understanding Option Method

For the following map signature, am I reading it correctly? object OptionImpl extends Option { def map[B](f: A => B): Option[B] } source - FP in Scala [B] means only objects of type B can call this function f: A => B means that it…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
0
votes
4 answers

How do I rewrite this conditional with an option in Scala in a cleaner way?

Is there a cleaner way of writing this in scala? def myFunction(somethingA: String, somethingB: Option[String]): Unit = if (somethingB.isDefined) foo("somethingA" -> somethingA, "somethingB" -> somethingB.get) else foo("somethingA" ->…
Donuts
  • 2,185
  • 3
  • 20
  • 31
1 2 3
15
16