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:
sealed class Option<out T : Any> {
object None : Option<Nothing>()
data class Some<out T : Any>(val value: T) : Option<T>()}
so I can easily create a map functor:
fun <T : Any, B : Any> Option<T>.map(f: (T) -> B): Option<B> =
when (this) {
is Option.None -> Option.None
is Option.Some -> Option.Some(f(this.value))}
Is this something I can achieve in Java? Im not concerned about the lack of extentions methods, I can live 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...