Dotty: TASTY: no way to access type constructor of an `AppliedType` and explore its argument variance

Created on 11 Mar 2020  路  4Comments  路  Source: lampepfl/dotty

This is another problem which blocks izumi-reflect reimplementation for Dotty.

Let's assume that we have the following definitions:

trait X[+_]
trait A[+F[+_]]

Having AppliedType for A[X[Int]] I need to be able to access it's type constructor, enumerate parameters and obtain variance flags. Essentially I need to get something like List(TypeParameter(F, Covariant))

So far I've found the only (insane) way to access something which looks like a type constructor:

val tycontree = a.tycon.asInstanceOf[TypeRef].typeSymbol.tree.asInstanceOf[TypeDef].rhs.asInstanceOf[TypeTree]

            val params = try {
              tycontree match {
                case d: {def constr: DefDef} =>
                  d.constr.typeParams
                case o =>
              }
            } catch {
              case t: Throwable =>
            }

Though this way I cannot extract type parameter declarations - all the values returned by typeParams are instances of TypeBounds with no variance information attached.

See also: 7mind/dotty-typetag-research#2

metaprogramming

Most helpful comment

@nicolasstucki If I have a symbol for trait A[+F[+_]], I can call SymDenotation#typeParams to get the symbol of F, then check the .is(Covariant) / .is(Contravariant) flags on that symbol.

(If I want the variance of the parameter of F itself, then I need to get the info of F which should be a TypeBounds whose upper-bound is an HKTypeLambda, once I get that I can finally call HKTypeLambda#declaredVariances on it.)

All 4 comments

@nicolasstucki could you check this issue please?

@smarter how would we do this properly within the compiler?

@nicolasstucki If I have a symbol for trait A[+F[+_]], I can call SymDenotation#typeParams to get the symbol of F, then check the .is(Covariant) / .is(Contravariant) flags on that symbol.

(If I want the variance of the parameter of F itself, then I need to get the info of F which should be a TypeBounds whose upper-bound is an HKTypeLambda, once I get that I can finally call HKTypeLambda#declaredVariances on it.)

As for #8514, big 馃憤 on this from me and other ZIO contributors / users eager to fully utilize the new environment encoding on Scala 3.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

odersky picture odersky  路  3Comments

LaymanMergen picture LaymanMergen  路  3Comments

ohze picture ohze  路  3Comments

adamgfraser picture adamgfraser  路  3Comments

andreaTP picture andreaTP  路  3Comments