12
votes

Scala has path-dependent types, but it is said that Scala doesn’t support dependent typing. What is the difference between path-dependent types and dependent types?

As far as I understand, path-dependent types are one kind of dependent types.

1
See Miles Sabin's answer here to a similar question (yours is a much better question, so I'm not marking as a duplicate).Travis Brown

1 Answers

6
votes

A dependent type is a type that depends on a value. A path dependent type is a specific kind of dependent type in which the type depends on a path.

I am not sure if the term "path dependent type" exists outside the Scala community. In any case, the question is, what is a path? For Scala, this is defined in the language specification: basically it's a sequence of selections a.b.c... on non-variable values.

A path dependent type is a type with a path, for example a.T in

class A { type T; def f: T }
def f(a: A): a.T = a.f

There are other kinds of dependent types. For example, in Scala, the is a pending proposal to add literal-based types to the language, so that you could write val x: 42.type = 21 + 21.

In order to type check a program that uses dependent types, the type system (and the compiler) needs to know about the semantics of these values and its operations. The Scala compiler knows the semantics of selection and can decide whether two paths are the same or not. For the example using literal-based types, the compiler needs to be extended to know what the + operation on integer means.