1

Assuming that several objects in Scala share an identical definition of nested generic class or trait H, with only 1 Peer type defined using F-bounded polymorphism:

object Example {

  trait P {

    type H[Peer <: H[Peer]]
  }

  object P1 extends P {
    trait H[Peer <: H[Peer]]
  }
  object P2 extends P {
    trait H[Peer <: H[Peer]]
  }

//  object KO extends P1.HH
}

This compiles with absolutely no problem in Scala 2.13.10. But on Scala 3.2.1 it causes the following error:

....scala:25:20: Cyclic reference involving type H
one error found

What may have caused this definition to violate the underlying type calculus? Can the type system still regard as GADT-friendly if such definition can't be supported?

tribbloid
  • 4,026
  • 14
  • 64
  • 103
  • probably related to this change: https://dotty.epfl.ch/docs/internals/higher-kinded-v2.html – tribbloid Jan 06 '23 at 19:22
  • If `P` is an object, it will be an illegal self-referencing type, however P is not concrete, and the compiler can delay resolution of `H[_]` when P1 & P2 are compiled – tribbloid Jan 06 '23 at 19:50
  • 2
    It is not enough that `H` will eventually be resolved at a definition, since the use-side must be considered. Say you have `val x: P`. Then `x.H`'s "kind" is basically `[Peer <: x.H[Peer]] => Type`. The kind contains a non-concrete type `x.H` (it can *never* be resolved to a `class` or `trait`). This is worrying. Scalac goes ahead and assumes it will be fine. Dotty plays it safe and doesn't. I am not sure the underlying (full!) type calculus of Dotty is written down/fixed anywhere. – HTNW Jan 06 '23 at 20:23
  • Not it is is not written, the DOT logic (or should I call it calculus?) doesn't differentiate between type & trait/class, which makes it even more worrying as GADT works only for traits but not types! – tribbloid Jan 06 '23 at 20:37
  • hmm ... so apparently it is already on the roadmap: https://dl.acm.org/doi/10.1145/3486610.3486892 – tribbloid Jan 06 '23 at 21:04
  • @tribbloid https://www.youtube.com/watch?v=VV9lPg3fNl8 – Dmytro Mitin Jan 07 '23 at 00:12
  • https://github.com/scala/bug/issues?q=is%3Aissue+is%3Aopen+Cyclic+reference+involving https://github.com/lampepfl/dotty/issues?q=is%3Aissue+is%3Aopen+Cyclic+reference+involving – Dmytro Mitin Jan 07 '23 at 00:16
  • 1
    @tribbloid Actually, in Scala 2.13 the code stops to compile too, just a little later, when you start to apply it https://scastie.scala-lang.org/DmytroMitin/d2HghpuwQBihs2o0Mp1oPA/1 – Dmytro Mitin Jan 07 '23 at 00:22
  • @tribbloid Why do you consider such kind of definitions useful for GADT? – Dmytro Mitin Jan 07 '23 at 00:25
  • @DmytroMitin good question, how about representing inductive conjectures? `type Prime {type Factorial#PlusOne <: Prime}` – tribbloid Jan 14 '23 at 21:44

0 Answers0