-
Notifications
You must be signed in to change notification settings - Fork 1.1k
ValueOf typeclass to inhabit singleton types #2616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
It looks like this feature has been added in Scala 2.13.0-M3 (scala.ValueOf), when will we add it to Dotty? |
It should probably be implemented as another fallback case in implicit search. @Glavo I think it could be a good first issue if you want to give it a shot! |
I have an implementation, but I haven't made a pr for it yet |
I'll pick this up if nobody else wants it. |
PR here: #5647. |
Shouldn't this also work for Tuple Singletons? scala> valueOf[("orders", "trades")];;
1 |valueOf[("orders", "trades")];;
|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|cannot reduce summonFrom with
| patterns : case ev @ _:ValueOf[(("orders" : String), ("trades" : String))]
| This location contains code that was inlined from Predef.scala:25 Use case would be making type-safe channel subscription? case class TradeEvent()
case class OrderEvent()
type Channel = "trades" | "orders"
type Trades = "trades"
type Orders = "orders"
type SelChannel[C] = C match {
case Trades => TradeEvent
case Orders => OrderEvent
case x *: xs => SelChannel[x] | SelChannel[xs]
case EmptyTuple => Nothing
}
def subscribeOne[Channel <: Singleton](): List[SelChannel[Channel]] =
val channels: List[String] = List(valueOf[Channel]: String)
???
def subscribe[Channels <: Tuple](): List[SelChannel[Channels]] =
val channels: List[String] = (valueOf[Channels]: Tuple).toList
???
val orderSubscription: List[OrderEvent] = subscribeOne[("orders")]()
val orderSubscription2: List[OrderEvent] = subscribe[Tuple1["orders"]]()
val allSubscription: List[TradeEvent | OrderEvent] = subscribe[("orders", "trades")]() |
Edit: comment moved to lampepfl/dotty-feature-requests#172 (comment) |
Currently there is no way to obtain the runtime value corresponding to a singleton type. SIP-23 - Literal-based singleton types mentions a
ValueOf
(orSingleInhabitant
) type class that is currently missing from Dotty.For reference, a
ValueOf
type class was implemented in Typelevel Scala in this commit:typelevel/scala@d777d40
The text was updated successfully, but these errors were encountered: