What would such types expand to? Extension methods are just normal methods. But if I apply their translation to extension types I would get
type Extended(f: F)
That’s not valid type syntax, or semantics. Types cannot take values as parameters. Maybe they should, that’s a good area to work on which gets us more on the road to dependent types. But that work needs to come first. Afterwards we can dream about type extensions.
Your message talks about extension type Extended(f: F) or something like that which is out of scope for the question which is asking for extension [Typ](t: Typ) type ListOf = List[Typ] which is just a type member for Typs eg type IntList = Int.ListOf
No, you are still on the surface. Have a look at the definition of extension methods. Extension Methods. It contains a section “translation” at the very top. We need something analogous for extension types.
It appears that the original example should desugar to something like this:
trait Foo
trait Bar[T]
erased def extension_F[F <: Foo](f: F): { type Extended = Bar[F] } = erasedValue
trait SpecificFoo[T] extends Foo
val f = new SpecificFoo[5]{}
val extended$f = extension_F(f)
summon[extended$f.Extended =:= Bar[SpecificFoo[5]]]
Since the original example includes a value parameter, it should generate a method to return a type with the type member that initiated an extension type member search
If only we could somehow generalize extension blocks to make them behave more uniformly with the rest of the language… Something more like a class… An extension class? Or shall we say an implicit class since it’s constructed implicitly?
Yeah, you can do a similar thing with implicit classes, but you can’t get rid of the need for an explicit val assignment
trait Foo
trait Bar[T]
implicit class extension_F[F <: Foo](f: F) {
object Extended { type T = Bar[F] }
}
trait SpecificFoo[T] extends Foo
val f = new SpecificFoo[5]{}
val ext = f.Extended
@main def main = implicitly[ext.T =:= Bar[SpecificFoo[5]]]
In Scala 2 f.Extended.T doesn’t work because implicit search doesn’t start inside types, in Scala 3 it does start, but it’s not a stable path so you can’t select from it main$package.extension_F[F]#Extended.type is not a valid type prefix, since it is not an immutable path