Skip to content

Instantly share code, notes, and snippets.

@Swoorup
Created February 3, 2021 06:00
Show Gist options
  • Save Swoorup/5795be94b8998d3ddd51bce51b92413e to your computer and use it in GitHub Desktop.
Save Swoorup/5795be94b8998d3ddd51bce51b92413e to your computer and use it in GitHub Desktop.
Scala 3 Match types Channel subscription
case class TradeEvent()
case class OrderEvent()
type Channel = "trades" | "orders"
// def sub2[Channels <: Tuple](channels: Channels)(using Channel <:< Tuple.Union[Channels]) = channels.toList
type SelChannel[C <: Tuple] = C match {
case "trades" *: xs => TradeEvent | SelChannel[xs]
case "orders" *: xs => OrderEvent | SelChannel[xs]
case _ => Nothing
}
def subscribe[Channels <: Tuple](channels: Channels)
(using Channel <:< Tuple.Union[Channels]): List[SelChannel[Channels]] =
???
val tradeSubscription: List[TradeEvent] = subscribe(Tuple1("trades"))
val orderSubscription: List[OrderEvent] = subscribe(Tuple1("orders"))
val allSubscription: List[TradeEvent | OrderEvent] = subscribe(("orders", "trades"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment