0

I am new to Scala and searched for the same as to how can we change from Option[String] to a Map[String,trait] but could not find much .

The thing is I have a field of type Option[String] and I have to pass that value to a case class which takes input as a Map[String,User Defined trait].

That's what I want to know as to how can I convert the value of Option[String] to Map[]

Can we use Option.fold in this?

Could you please guide me further on this.

TIA

Tim
  • 26,753
  • 2
  • 16
  • 29
neil
  • 149
  • 1
  • 12

2 Answers2

1

Consider using toMap as shown in the following example:

trait UDTrait[A] {
  def len(s: A): Int
}

case class MyClass(m: Map[String, UDTrait[String]])

def myOptionToMap(opt: Option[String]): Map[String, UDTrait[String]] =
  opt.map((_, new UDTrait[String]{ def len(s: String) = s.length })).toMap

MyClass(myOptionToMap(Some("a")))
// res1: MyClass = MyClass(Map(a -> $anonfun$myOptionToMap$1$$anon$1@10aabada))

MyClass(myOptionToMap(None))
// res2: MyClass = MyClass(Map())

Alternatively, you can use fold, as follows:

def myOptionToMap(opt: Option[String]): Map[String, UDTrait[String]] =
  opt.fold(Map.empty[String, UDTrait[String]])(s =>
    Map(s -> new UDTrait[String]{ def len(s: String) = s.length })
  )
Leo C
  • 22,006
  • 3
  • 26
  • 39
  • Thanks for the explanation. Can you explain me how the above fold myOptionToMap method works.. I have been trying to understand the working of fold but didn't understand much. – neil Oct 19 '18 at 18:27
  • I used fold in my code but it is giving an error and telling to write implemented methods. What could be the reason? When I implement those methods, even the error persists. – neil Oct 19 '18 at 18:58
  • I added that, but even after that it is giving an error. Could you explain me the working of fold function – neil Oct 19 '18 at 19:26
  • As described in the provided API doc, `fold` has the signature `def fold[B](ifEmpty: => B)(f: (A) => B): B`. If the option is non-empty, `fold` applies the `s => Map(s -> new UDTrait{})` function to the option's value; otherwise it returns the value of the `ifEmpty` expression which is `Map.empty[String, UDTrait]`. – Leo C Oct 19 '18 at 19:43
  • What is the meaning of trait UDTrait[A <: Any] ? How can I initialize A here? If I am writing any value on A part and error says "Overriding type Any does not conform to base type A"? How do I resolve this? – neil Oct 19 '18 at 20:16
  • @neil, please see if the revised answer addresses your additional question; if not, I'd suggest that you edit your original question to supplement with a minimal version of sample code along with expected sample result. – Leo C Oct 19 '18 at 21:43
0

Option[ T ] is a container for zero or one element of a given type. An Option[T] can be either Some[T] or None object, which represents a missing value.

A Map is an Iterable consisting of pairs of keys and values. If you want to construct a map with one key value pair from an option which is non-empty, you can do the following:

trait Foo

def convertNonEmptyOptionToMap(a:Some[String], t: Foo): Map[String, Foo] = Map(a.get -> t)

That said, I don't completely understand what you're trying to do. More context with examples would be helpful.

  • 2
    If you want your method to accept a non-empty `Option` then make your parameter a `Some`. Or, you can accept a potentially-empty `Option` and use code like `a.map(x => Map(x, t)).getOrElse(Map.empty)`. – Hosam Aly Oct 19 '18 at 16:37