Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Minimal alternative for scala-reflect and TypeTag #62

pshirshov opened this issue Aug 11, 2019 · 3 comments

Minimal alternative for scala-reflect and TypeTag #62

pshirshov opened this issue Aug 11, 2019 · 3 comments


Copy link

pshirshov commented Aug 11, 2019


Yesterday we had a discussion with Miles Sabin and he given us advice to open a ticket here.

We have a project, distage. It's a module system with an automatic solver (or you may call it a staged dependency injection mechanism with a garbage collector). It heavily relies on TypeTags, particularly on =:= and <:<.

Unfortunately, scala-reflect suffers from some concurrency bugs, so we've implemented our own lightweight type tags which do not depend on scala-reflect in run time. In foreseeable future I'm going to try to port this to Dotty, it seems to be doable.

I've reflected my experience of implementing it in a blog post.

At this moment Dotty does not provide any runtime alternative to TypeTag. Though it's a very important and very attractive feature. It would be great if we may have access to Tasty data in run time. And/or any simple mechanism which may provide us =:= and <:< would be very convenient.

It may possible to do it outside of the compiler but it's a very complicated job and whatever we do the outcome would be less precise than we may achieve using compiler data.

May it be possible to consider implementing TypeTags supporting at least equality and subtype checks in Dotty?

Discussion topic on

Copy link

gzoller commented Sep 13, 2019

We need to take this much farther than just equality. For example ScalaJack's JSON serialization of traits:

trait Pet{ name: String }
case class Cat(name: String, isPurring: Boolean) extends Pet
case class Dog(name: String) extends Pet
case class Pets(all:List[Pet])

val pets = Pets(List(Cat("Slinky",true),Dog("Spot"))
val sj = ScalaJack()
val js = sj.render(p) 
// renders:  {"all":[{"_hint":"com.mypackage.Cat","name":"Slinky","isPurring":true},{"_hint":"com.mypackage.Dog","name":"Spot"}]}
val orig =[Pets](js)  // rebuilds original object from JSON

At runtime it needs to look at each Pet in the list and discern its concrete type, members, etc. Other needed reflections include parameterized types (where the type parameters may be implementations of a trait--where Thing[T] and T is a trait).

Rather than focus on scala-reflect's shortcomings on the edges, let's focus on the good things it does provide. I'd gladly accept something similar, even if they came with the same imperfections. TypeTags are good 'nuf for most uses.

If the desire is to hide some of these internals, it'd be ok to have abstractions like in Java's reflection, but more Scala-specific, and with a lot more detail.

Copy link

pshirshov commented Feb 25, 2020

For those who acidentally land here:

  1. We made a "good enough" TypeTag implementation for Jvm/JS/Native 2.xx:
  2. We are going to port it to Dotty soon, looks like there are enough Tasty APIs to make the port possible

Copy link

gzoller commented Feb 25, 2020

This is super! Can’t wait to play with it, and I’m excited about the prospect of a dotty port. I’ve been approaching the problem from the direction of Tasty inspection, which I intend to marry to runtime JVM reflection for a full view. Project here:
VERY rough and preliminary but it does support new dotty features like union/intersection types, match types, and opaque type aliases.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
None yet

No branches or pull requests

2 participants