When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableTemplate?

前端 未结 3 1231
臣服心动
臣服心动 2020-12-02 14:20

@uncheckedVariance can be used to bridge the gap between Scala\'s declaration site variance annotations and Java\'s invariant generics.

scala>         


        
相关标签:
3条回答
  • 2020-12-02 14:58

    The problem is that GenericTraversableTemplate is used twice: once for mutable collections (where its type parameter should be invariant), and once for immutable collections (where covariance is invariably king).

    GenericTraversableTemplate's typechecks assuming either covariance or invariance for the A type parameter. However, when we inherit it in a mutable trait, we have to pick invariance. Conversely, we'd like covariance in an immutable subclass.

    Since we can't abstract over the variance annotation (yet ;-)) in GenericTraversableTemplate, so that we could have instantiated it to either one depending on the subclass, we have to resort to casting (@uncheckVariance is essentially a kind-cast). For further reading, I recommend my dissertation (sorry ;-)) or our recent bitrot paper

    0 讨论(0)
  • 2020-12-02 15:01

    I found another time where @uncheckedVariance is used -- the synthetic method that returns the default value for a parameter of an abstract type:

    M:\>scala -Xprint:typer -e "class C { def p[T >: Null](t: T = null) = t }"
    [[syntax trees at end of typer]]// Scala source: (virtual file)
    package <empty> {
      final object Main extends java.lang.Object with ScalaObject {
        def this(): object Main = {
          Main.super.this();
          ()
        };
        def main(argv: Array[String]): Unit = {
          val args: Array[String] = argv;
          {
            final class $anon extends scala.AnyRef {
              def this(): anonymous class $anon = {
                $anon.super.this();
                ()
              };
              class C extends java.lang.Object with ScalaObject {
                <synthetic> def p$default$1[T >: Null <: Any]: Null @scala.annotation.unchecked.uncheckedVariance = null;
                def this(): this.C = {
                  C.super.this();
                  ()
                };
                def p[T >: Null <: Any](t: T = null): T = t
              }
            };
            {
              new $anon();
              ()
            }
          }
        }
      }
    
    0 讨论(0)
  • 2020-12-02 15:09

    In my thesis I describe a calculus, Scalina, that has bounds & variance annotations as part of the kind language (an earlier version is also available as a workshop paper). The relevance to this discussion is the next step that I want to take in developing this calculus: build another layer on top of that so that you can abstract over bounds (easy) and variance annotations (makes my head spin). Actually, you wouldn't just tack 1 extra layer on there, but rather generalise your polymorphism constructs so they work at all levels, and make your "attributes" (bounds, variance annotations, required implicit arguments,...) into regular types with special kinds, which are all subject to abstraction.

    The "attributes are types" idea is explained nicely by Edsko de Vries in the context of uniqueness types.

    Uniqueness Typing Simplified, Edsko de Vries, Rinus Plasmeijer, and David Abrahamson. In Olaf Chitil, Zoltán Horváth and Viktória Zsók (Eds.): IFL 2007, LNCS 5083, pp. 201-218, 2008.

    Abstract: We present a uniqueness type system that is simpler than both Clean's uniqueness system and the system we proposed previously. The new type system is straightforward to implement and add to existing compilers, and can easily be extended with advanced features such as higher rank types and impredicativity. We describe our implementation in Morrow, an experimental functional language with both these features. Finally, we prove soundness of the core type system with respect to the call-by-need lambda calculus.

    0 讨论(0)
提交回复
热议问题