Currently, Scala has weird function class notation such as Function1[A, B]
. For normal types, this is fine. However, if we want to express some complex path dependent types such as (x: A) => x.T
, we may have to write Function1[A, A#T]
. Is this notation suitable for dependent types? p.s. I also noticed that type projections may be dropped in a way in the future.
Also I noticed that it’s a little hard for the new design of implicit conversion in Scala3 to express implicit conversions containing dependent types. I’m not sure if I can use type projections safely.
In scala 3 you can just use the dependent function type as you described:
trait Box {
type T
val t: T
}
val f: (x: Box) => x.T = x => x.t
If you want to extend this to other class types, you can use a refinement,
e.g.
trait Fun[-A, +B]:
def apply(x: A): B
val m: Fun[Box, Box#T] { def apply(x: Box): x.T } =
x => x.t
val box = new Box { type T = Int; val t = 23}
val z: box.T = m(box) // path dependent type is preserved
2 Likes
This is helpful. Thank you! It’s good to know that scala3 still supports type projections on type alias.
Now I know how to write implicit conversions with dependent types in Scala3.
It actually does not. This type projection works because the prefix is a concrete type Box
.