From the canonical "Programming in Scala" from Oderskey and Venners:
scala> val results = List(Some("apple"), None,
Some("orange"))
results: List[Option[java.lang.String]] = List(Some(apple),
None, Some(orange))
scala> for (Some(fruit) <- results) println(fruit)
apple
orange
I do not understand the philosophy of scala to impose on the programmer the need to explicitly mention Some(apple) as opposed to inferring it. I would prefer to write/see the following:
scala> val results = List("apple", None, "orange")
results: List[Option[java.lang.String]] = List(apple, None, orange)
scala> for (fruit <- results) println(fruit)
apple
orange
Or maybe at the least the following (providing typing at the list level but not at the individual item level):
scala> val results :List[Option[String]] = ("apple", None, "orange")
results: List[Option[java.lang.String]] = List(apple, None, orange)
scala> for (fruit <- results) println(fruit)
apple
orange
At least in this last case: the type of the List is being provided (to help the compiler..) but we still avoid the verbosity of "boxing" every element in the List like Some('foo').
Anyone out there who is better in tune with scala's way of thinking can tell me why I should have to do that extra typing .. ?
Edit: so the following does what I want for Strings.
scala> implicit def toOpt(a : String) = Some(a)
toOpt: (a: String)Some[String]
scala> val myList : List[Option[String]] = List("first", None, "third")
myList: List[Option[String]] = List(Some(first), None, Some(third))
If someone can show how to generalize the above using higher kinded types I will award the answer.