title: Collection of Union types scala
body: <p>Is it possible in scala to have a collection of a union types. There are a few approaches to union types discussed <a href="https://stackoverflow.com/questions/3508077/how-to-define-type-disjunction-union-types">here</a> The top rated answer feels the most native, i have something like this: </p>
  
  <pre><code>sealed trait StringOrNumber[T]
  object StringOrNumber {
      implicit object IntWitness extends StringOrNumber[Int]
      implicit object StringWitness extends StringOrNumber[String]
  }
  </code></pre>
  
  <p>but when i try to make a map that contains both </p>
  
  <pre><code>val m: Map[String, Any] = Map("str" -&gt; "hellp", "int" -&gt; 32)
  </code></pre>
  
  <p>The scala compiler sees it as a map of [String,Any] Is there a way to tell the scala compiler this is a map [String, StringOrNumber]</p>
  
  <p>Edit:</p>
  
  <p>I dont think using the approach above is possible to create a collection of string or union. I think it needs to be another approach to a union type since the above is akin to overloaded methods rather than a true union type in the type system</p>