Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is is idiomatic scala to be explicit/verbose about Some[x[/Option[x]?
    primarykey
    data
    text
    <p>From the canonical "Programming in Scala" from Oderskey and Venners:</p> <pre><code>scala&gt; val results = List(Some("apple"), None, Some("orange")) results: List[Option[java.lang.String]] = List(Some(apple), None, Some(orange)) scala&gt; for (Some(fruit) &lt;- results) println(fruit) apple orange </code></pre> <p>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:</p> <pre><code>scala&gt; val results = List("apple", None, "orange") results: List[Option[java.lang.String]] = List(apple, None, orange) scala&gt; for (fruit &lt;- results) println(fruit) apple orange </code></pre> <p>Or maybe at the least the following (providing typing at the list level but not at the individual item level): </p> <pre><code>scala&gt; val results :List[Option[String]] = ("apple", None, "orange") results: List[Option[java.lang.String]] = List(apple, None, orange) scala&gt; for (fruit &lt;- results) println(fruit) apple orange </code></pre> <p>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').</p> <p>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 .. ?</p> <p>Edit: so the following does what I want for Strings. </p> <pre><code>scala&gt; implicit def toOpt(a : String) = Some(a) toOpt: (a: String)Some[String] scala&gt; val myList : List[Option[String]] = List("first", None, "third") myList: List[Option[String]] = List(Some(first), None, Some(third)) </code></pre> <p>If someone can show how to generalize the above using higher kinded types I will award the answer.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload