Scala 3 - type parameters and type members

There was mention in the past about some form of unification between type parameters and type members in Scala 3, as a means to simplify the language. I have not seen any recent mentions about this effort, nor does it seem featured in the Scala 3 roadmap.

I’m just wondering if this is still an ongoing research or whether it is something that was abandoned or turned out to be impractical, unsound or otherwise undesirable.

Thank you.

(Sorry, I looked for a reference to this effort but came up short with Google; I believe this was previously covered in one of Martin’s talks about Scala 3 / Dotty.)

4 Likes

Hello, type parameters are indeed encoded as abstract type members (with name mangling), you can read more in this paper:

http://guillaume.martres.me/publications/dotty-hk.pdf,

Although this may be out of date again

We went back having type parameters as first class feature. We learned a lot trying to do the encoding but in the end it was too brittle and too hard to make 100% backwards compatible, so we abandoned the effort.

10 Likes

There are still 2 problems though:

  1. The old design doc is still on the latest language reference (Higher-Kinded Types in Dotty), and ranked pretty high on Google search.

  2. Does the old design proposed a way to encode covariant/contravariant modifiers into type members? I was trying to manually convert it using type members:

before:

  object AsArg {

    trait P[+TT] {
      val vv: TT
    }

    trait P1 extends P[Product] {
      val vv: Product
    }
    trait P2 extends P1 with P[Tuple1[Int]] {
      val vv: Tuple1[Int]
    }
  }

after:

  object AsDependentType {

    trait P {
      type TT

      val vv: TT
    }

    trait P1 extends P {
      type TT <: Product

      val vv: Product
    }

    trait P2 extends P1 with P {
      type TT <: Tuple1[Int]

      val vv: Tuple1[Int]
    }
  }

But it never works and apparently is incompatible with the new type logic.

(main discussion: scala - In Scala3, if generic type argument(s) is mapped to dependent type, how are covariant & contravariant modifiers mapped? - Stack Overflow)

In case somebody would find it helpful, here are slides that explain the history behind encoding type parameters as type members and why it hasn’t worked out in the end:

(sadly it seems like we don’t have recording from the talk)

7 Likes

Yeah its very helpful, never thought it would be tied to the predicativeness of the language.