Skip to content
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

Minimal alternative for scala-reflect and TypeTag #62

Open
pshirshov opened this issue Aug 11, 2019 · 3 comments
Open

Minimal alternative for scala-reflect and TypeTag #62

pshirshov opened this issue Aug 11, 2019 · 3 comments

Comments

@pshirshov
Copy link

@pshirshov pshirshov commented Aug 11, 2019

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

@gzoller
Copy link

@gzoller 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 = 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.

@pshirshov
Copy link
Author

@pshirshov 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: 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

@gzoller
Copy link

@gzoller 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: 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.

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

Successfully merging a pull request may close this issue.

None yet
2 participants