title: Union types in Java
body: <p>I've been working with C# for a while and trying to get more familiar with Java. So I'm trying to migrate some of the basic patterns I use on daily basis in C# even only to understand the gap between JVM and dotnet and figure out how to deal with them. Here is the first problem I encountered - an option type - somethiong which is quite easy to achieve in many languages i.e. Koltlin: </p>
  
  <pre><code>sealed class Option&lt;out T : Any&gt; {
      object None : Option&lt;Nothing&gt;()
      data class Some&lt;out T : Any&gt;(val value: T) : Option&lt;T&gt;()}
  </code></pre>
  
  <p>so I can easily create a map functor:</p>
  
  <pre><code>fun &lt;T : Any, B : Any&gt; Option&lt;T&gt;.map(f: (T) -&gt; B): Option&lt;B&gt; =
      when (this) {
          is Option.None -&gt; Option.None
          is Option.Some -&gt; Option.Some(f(this.value))} 
  </code></pre>
  
  <p>Is this something I can achieve in Java? Im not concerned about the lack of extentions methods, I can leave without that, but how to perform the actual type matching without having to rely on an unchecked cast? At least thats what IntelliJ is complaining about...</p>