Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In this case you can use <a href="http://www.scala-lang.org/api/current/index.html#scala.Either" rel="nofollow">scala.Either</a></p> <pre><code>scala&gt; val map: Map[String, Either[String, () =&gt; String]] = Map.empty map: Map[String,Either[String,() =&gt; String]] = Map() scala&gt; map + ("key" -&gt; Left("value")) res0: scala.collection.immutable.Map[String,Either[String,() =&gt; String]] = Map(key -&gt; Left(value)) scala&gt; res0("key") res1: Either[String,() =&gt; String] = Left(value) scala&gt; map + ("key2" -&gt; Right(() =&gt; "value2")) res2: scala.collection.immutable.Map[String,Either[String,() =&gt; String]] = Map(key2 -&gt; Right(&lt;function0&gt;)) scala&gt; res2("key2") res3: Either[String,() =&gt; String] = Right(&lt;function0&gt;) </code></pre> <h3>Update</h3> <p>You can hide implementation from caller using something like this.</p> <pre><code>def toEither[T &lt;: Any : Manifest](x: T): Either[String, () =&gt; String] = x match { case x: String =&gt; Left(x) case x: Function0[String] if manifest[T] &lt;:&lt; manifest[() =&gt; String] =&gt; Right(x) case _ =&gt; throw new IllegalArgumentException } </code></pre> <p>The actual type of <code>Function0</code> is eliminated due type erasure, but it can be verified with <code>Manifest</code></p> <pre><code>scala&gt; map + ("key" -&gt; toEither("value")) res1: scala.collection.immutable.Map[String,Either[String,() =&gt; String]] = Map(key -&gt; Left(value)) scala&gt; map + ("key2" -&gt; toEither(() =&gt; "value2")) res2: scala.collection.immutable.Map[String,Either[String,() =&gt; String]] = Map(key2 -&gt; Right(&lt;function0&gt;)) scala&gt; res2("key2").right.get() res3: String = value2 scala&gt; map + ("key2" -&gt; toEither(() =&gt; 5)) java.lang.IllegalArgumentException scala&gt; map + ("key2" -&gt; toEither(false)) java.lang.IllegalArgumentException </code></pre> <h3>Update2</h3> <p>As @Submonoid rightly corrected me in the comments below, there is much simpler way of dealing with <code>Either</code>.</p> <pre><code>type StringOrFun = Either[String, () =&gt; String] implicit def either(x: String): StringOrFun = Left(x) implicit def either(x: () =&gt; String): StringOrFun = Right(x) val m: Map[String, StringOrFun] = Map("key" -&gt; "value", "key2" -&gt; (() =&gt; "value2")) </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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