1

What do I need the type of x to be in the following code to be able to refer to x.f in g()? A.B didn't work.

trait A {
    trait B {
        def f: Unit
    }
}

object O {
    def g(x : ?): Unit {
        x.f
    }
}
Dmytro Mitin
  • 48,194
  • 3
  • 28
  • 66
Clinton
  • 22,361
  • 15
  • 67
  • 163
  • 2
    Possibly useful: [Implementing inner traits in Scala like we do with inner interfaces in Java](https://stackoverflow.com/questions/15614849/implementing-inner-traits-in-scala-like-we-do-with-inner-interfaces-in-java) – Chris Dec 14 '21 at 06:58
  • 6
    `def g(x: A#B): Unit = { x.f }` – jwvh Dec 14 '21 at 07:44
  • Why would you need that? – Matthias Berndt Dec 14 '21 at 13:57
  • @jwvh Or with a finer type `def g(x: a.B forSome {val a: A}): Unit = { x.f }` – Dmytro Mitin Dec 14 '21 at 20:29
  • @DmytroMitin existential types [are no longer](https://dotty.epfl.ch/docs/reference/dropped-features/existential-types.html) supported in scala 3 :) I think projection (A#B) it is better for now, isn't? – gianluca aguzzi Dec 14 '21 at 23:43
  • 1
    @gianlucaaguzzi It's not clear whether OP asks about Scala 2 or Scala 3. Existential types (not wildcarded) are no longer supported. General projections (with abstract `A`) are not either (for traits `A#B` is ok). Projections are not better or worse than existentials, they are just different. – Dmytro Mitin Dec 15 '21 at 15:10
  • @gianlucaaguzzi Scala 2.12, although I'd rather not add in features that are going to be deprecated in Scala 3 (I'd like to upgrade at some point) – Clinton Dec 16 '21 at 00:17

0 Answers0