dotty-feature-requests icon indicating copy to clipboard operation
dotty-feature-requests copied to clipboard

Minimal alternative for scala-reflect and TypeTag

Open pshirshov opened this issue 6 years ago • 3 comments
trafficstars

Hi,

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 contributors.scala-lang.org

pshirshov avatar Aug 11 '19 22:08 pshirshov

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 = sj.read[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.

gzoller avatar Sep 13 '19 14:09 gzoller

For those who acidentally land here:

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

pshirshov avatar Feb 25 '20 00:02 pshirshov

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: https://github.com/gzoller/dotty_reflection VERY rough and preliminary but it does support new dotty features like union/intersection types, match types, and opaque type aliases.

gzoller avatar Feb 25 '20 03:02 gzoller