|
| 1 | +--- |
| 2 | +layout: doc-page |
| 3 | +title: "Dependent Function Types - More Details" |
| 4 | +--- |
| 5 | + |
| 6 | +Initial implementation in (#3464)[https://github.com/lampepfl/dotty/pull/3464]. |
| 7 | + |
| 8 | +## Syntax |
| 9 | + |
| 10 | +FunArgTypes ::= InfixType |
| 11 | + | ‘(’ [ FunArgType {‘,’ FunArgType } ] ‘)’ |
| 12 | + | '(' TypedFunParam {',' TypedFunParam } ')' |
| 13 | +TypedFunParam ::= id ':' Type |
| 14 | + |
| 15 | +Dependent function types associate to the right, e.g. |
| 16 | +`(s: S) ⇒ (t: T) ⇒ U` is the same as `(s: S) ⇒ ((t: T) ⇒ U)`. |
| 17 | + |
| 18 | +## Implementation |
| 19 | + |
| 20 | +Dependent function types are shorthands for class types that define `apply` |
| 21 | +methods with a dependent result type.Dependent function types desugar to |
| 22 | +refinement types of `scala.FunctionN`. A dependent functon type |
| 23 | +`(x1: K1, ..., xN: KN) => R` of arity `N` translates to |
| 24 | + |
| 25 | + FunctionN[K1, ..., Kn, R'] { |
| 26 | + def apply(x1: K1, ..., xN: KN): R |
| 27 | + } |
| 28 | + |
| 29 | +where the result type parameter `R'` is the least upper approximation of the |
| 30 | +precise result type `R` without any referance to value parameters `x1, ..., xN`. |
| 31 | + |
| 32 | +The syntax and sementics of anonymous dependent functions is identical to the |
| 33 | +one of regular functions. Eta expansion is naturaly generalized to produce |
| 34 | +dependent function types for methods with dependent result types. |
| 35 | + |
| 36 | +Dependent functions can be implicit, and generalize to arity `N > 22` in the |
| 37 | +same way that other functions do, see [the corresponding |
| 38 | +documentation](https://dotty.epfl.ch/docs/reference/dropped/limit22.html). |
| 39 | + |
| 40 | +## Examples |
| 41 | + |
| 42 | +- (depfuntype.scala)[https://github.com/lampepfl/dotty/blob/0.10.x/tests/pos/depfuntype.scala] |
| 43 | + |
| 44 | +- (eff-dependent.scala)[https://github.com/lampepfl/dotty/blob/0.10.x/tests/run/eff-dependent.scala] |
| 45 | + |
| 46 | +### Type Checking |
| 47 | + |
| 48 | +After desugaring no additional typing rules are required for dependent function types. |
0 commit comments