Skip to content

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

Closed
OlivierBlanvillain opened this issue May 30, 2017 · 7 comments
Closed

ValueOf typeclass to inhabit singleton types #2616

OlivierBlanvillain opened this issue May 30, 2017 · 7 comments

Comments

@OlivierBlanvillain
Copy link
Contributor

Currently there is no way to obtain the runtime value corresponding to a singleton type. SIP-23 - Literal-based singleton types mentions a ValueOf (or SingleInhabitant) 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

@Glavo
Copy link
Contributor

Glavo commented May 30, 2018

It looks like this feature has been added in Scala 2.13.0-M3 (scala.ValueOf), when will we add it to Dotty?

@OlivierBlanvillain
Copy link
Contributor Author

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!

@smarter
Copy link
Member

smarter commented May 30, 2018

I have an implementation, but I haven't made a pr for it yet

@milessabin
Copy link
Contributor

I'll pick this up if nobody else wants it.

@milessabin milessabin self-assigned this Dec 18, 2018
@milessabin
Copy link
Contributor

PR here: #5647.

@Swoorup
Copy link

Swoorup commented Feb 3, 2021

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")]()

@bishabosha
Copy link
Member

bishabosha commented Feb 4, 2021

Edit: comment moved to lampepfl/dotty-feature-requests#172 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants