Here’s a proposal to add dependent function types to Scala 3. A dependent function type such as
(x: A) => x.B
is the logical first-class version of a dependent method
def m(x: A): x.B
Here’s a link to the doc page.
The main reason for adding dependent function types is more regularity. If methods can be dependent, why do you have to lift this into a class with an
apply method if you want to do the same for functions? It’s clearer and more efficient if we can express the argument dependency directly in the function type.
Originally, Scala did not have dependent function types because we did not see a good way to map them into normal Scala types. Adding a fundamentally new type to your type system is a big decision which should not be taken lightly. The Dotty implementation uses a clever scheme, where a dependent function type is represented as a normal function type with a refinement. This avoids essentially all the added complexity on the type system side.
This proposal is open for discussion in the community and will be discussed in our next SIP meeting, where we’ll take all your feedback into account for the approval/dismissal of this feature.
Once we have dependent function types, of course one would like to have polymorphic function types as well. There are some preliminary ideas about that floating around, but polymorphic function types should be a separate SIP, if they get to that stage.