Skip to content

Instantly share code, notes, and snippets.

@Sedose
Created September 5, 2024 23:20
Show Gist options
  • Save Sedose/36c6f05035e08d0dfd8e6090cc5b6cf6 to your computer and use it in GitHub Desktop.
Save Sedose/36c6f05035e08d0dfd8e6090cc5b6cf6 to your computer and use it in GitHub Desktop.
Union types. Exhaustive `match` (`switch` Java analog, `when` Kotlin analog). Without Java wrappers shit
@main def main(): Unit =
val value1 = true
val value2 = 1234L
val value3 = "Hello Scala 3!"
println(process(value1)) // "Received a Boolean: true"
println(process(value2)) // "Received a Long: 123"
println(process(value3)) // "Hello Scala 3!"
type ExternalType1 = java.lang.Boolean
type ExternalType2 = java.lang.Long
type ExternalType3 = java.lang.String
type CombinedType = ExternalType1 | ExternalType2 | ExternalType3
def process(value: CombinedType): String = value match {
case n: ExternalType1 => s"Received an Boolean: $n"
case s: ExternalType2 => s"Received a Long: $s"
case b: ExternalType3 => b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment