You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/_spec/03-types.md
+22-16Lines changed: 22 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -217,22 +217,27 @@ G[S, Int] // illegal: S constrains its parameter to
217
217
SimpleType ::= ‘(’ Types ‘)’
218
218
```
219
219
220
-
A _tuple type_ ´(T_1 , ... , T_n)´ is an alias for the class `scala.Tuple´n´[´T_1´, ... , ´T_n´]`, where ´n \geq 2´.
220
+
A _tuple type_ ´(T_1, ..., T_n)´ where ´n \geq 2´ is an alias for the type `´T_1´ *: ... *: ´T_n´ *: scala.EmptyTuple`.
221
+
222
+
Note:
223
+
`(´T´)` is just the type ´T´, and not `´T´ *: scala.EmptyTuple`.
224
+
`()` is not a valid type, and not `scala.EmptyTuple`.
225
+
226
+
If ´n \leq 22´, the type `´T_1´ *: ... *: ´T_n´ *: scala.EmptyTuple` is both a subtype and a supertype of tuple class `scala.Tuple´_n´[´T_1´, ..., ´T_n´]`.
221
227
222
228
Tuple classes are case classes whose fields can be accessed using selectors `_1`, ..., `_n`.
223
-
Their functionality is abstracted in a corresponding `Product` trait.
229
+
Their functionality is abstracted in the corresponding `scala.Product_´n´` trait.
224
230
The _n_-ary tuple class and product trait are defined at least as follows in the standard Scala library (they might also add other methods and implement other traits).
The type ´(T_1, ..., T_n) \Rightarrow U´ represents the set of function values that take arguments of types ´T_1, ..., Tn´ and yield results of type ´U´.
333
-
In the case of exactly one argument type ´T \Rightarrow U´ is a shorthand for ´(T) \Rightarrow U´.
334
-
An argument type of the form ´\Rightarrow T´ represents a [call-by-name parameter](04-basic-declarations-and-definitions.html#by-name-parameters) of type ´T´.
337
+
The type ´(T_1, ..., T_n) \Rightarrow R´ represents the set of function values that take arguments of types ´T_1, ..., Tn´ and yield results of type ´R´.
338
+
The case of exactly one argument type ´T \Rightarrow R´ is a shorthand for ´(T) \Rightarrow R´.
339
+
An argument type of the form ´\Rightarrow T´ represents a [call-by-name parameter](04-basic-declarations-and-definitions.md#by-name-parameters) of type ´T´.
335
340
336
-
Function types associate to the right, e.g. ´S \Rightarrow T \Rightarrow U´ is the same as ´S \Rightarrow (T \Rightarrow U)´.
341
+
Function types associate to the right, e.g. ´S \Rightarrow T \Rightarrow R´ is the same as ´S \Rightarrow (T \Rightarrow R)´.
342
+
343
+
Function types are [covariant](04-basic-declarations-and-definitions.md#variance-annotations) in their result type and [contravariant](04-basic-declarations-and-definitions.md#variance-annotations) in their argument types.
337
344
338
345
Function types are shorthands for class types that define an `apply` method.
339
-
Specifically, the ´n´-ary function type ´(T_1 , \ldots , T_n) \Rightarrow U´ is a shorthand for the class type `Function´_n´[´T_1´ , … , ´T_n´, ´U´]`.
340
-
Such class types are defined in the Scala library for ´n´ between 0 and 22 as follows.
346
+
Specifically, the ´n´-ary function type ´(T_1, ..., T_n) \Rightarrow R´ is a shorthand for the class type `Function´_n´[´T_1´, ..., ´T_n´, ´R´]`.
347
+
In particular ´() \Rightarrow R´ is a shorthand for class type `Function´_0´[´R´]`.
348
+
349
+
Such class types behave as if they were instances of the following trait:
341
350
342
351
```scala
343
-
packagescala
344
-
traitFunction´_n´[-´T_1´, ..., -´T_n´, +´U´] {
345
-
defapply(´x_1´: ´T_1´, ..., ´x_n´: ´T_n´): ´U´
346
-
overridedeftoString="<function>"
347
-
}
352
+
traitFunction´_n´[-´T_1´, ..., -´T_n´, +´R´]:
353
+
defapply(´x_1´: ´T_1´, ..., ´x_n´: ´T_n´): ´R´
348
354
```
349
355
350
-
Hence, function types are [covariant](04-basic-declarations-and-definitions.html#variance-annotations) in their result type and contravariant in their argument types.
356
+
Their exact supertype and implementation can be consulted in the [function classes section](./12-the-scala-standard-library.md#the-function-classes) of the standard library page in this document.
The anonymous function of arity ´n´, `(´x_1´: ´T_1, ..., x_n´: ´T_n´) => e` maps parameters ´x_i´ of types ´T_i´ to a result given by expression ´e´.
910
916
The scope of each formal parameter ´x_i´ is ´e´.
911
917
Formal parameters must have pairwise distinct names.
918
+
Type bindings can be omitted, in which case the compiler will attempt to infer valid bindings.
919
+
920
+
Note: `() => ´e´` defines a nullary function (´n´ = 0), and not for example `(_: Unit) => ´e´`.
912
921
913
922
In the case of a single untyped formal parameter, `(´x\,´) => ´e´` can be abbreviated to `´x´ => ´e´`.
914
923
If an anonymous function `(´x´: ´T\,´) => ´e´` with a single typed parameter appears as the result expression of a block, it can be abbreviated to `´x´: ´T´ => e`.
Copy file name to clipboardExpand all lines: docs/_spec/08-pattern-matching.md
+9-1Lines changed: 9 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -185,7 +185,15 @@ This is further discussed [here](#pattern-sequences).
185
185
SimplePattern ::= ‘(’ [Patterns] ‘)’
186
186
```
187
187
188
-
A _tuple pattern_`(´p_1´, ..., ´p_n´)` is an alias for the constructor pattern `scala.Tuple´n´(´p_1´, ..., ´p_n´)`, where ´n \geq 2´. The empty tuple `()` is the unique value of type `scala.Unit`.
188
+
A _tuple pattern_`(´p_1´, ..., ´p_n´)` where ´n \geq 2´ is equivalent to `´p_1´ *: ... *: ´p_n´ *: scala.EmptyTuple`.
189
+
190
+
Note:
191
+
`()` is equivalent to `_: scala.Unit`, and not `scala.EmptyTuple`.
192
+
`(´pat´)` is a pattern matching ´pat´, and not `´pat´ *: scala.EmptyTuple`.
193
+
194
+
Note:
195
+
As such patterns with `*:` are slow, a more efficient translation is free to be implemented.
196
+
For example, `(´p_1´, ´p_2´)` could be translated to `scala.Tuple2(´p_1´, ´p_2´)`, which is indeed equivalent to `´p_1´ *: ´p_2´ *: scala.EmptyTuple`.
Scala defines function classes `Function´n´` for ´n = 1 , \ldots , 22´.
306
-
These are defined as follows.
306
+
For each class type `Function´n´` where ´n = 0, ..., 22´, Scala defines the following function class:
307
307
308
308
```scala
309
309
packagescala
310
-
traitFunction´n´[-T_1, ..., -T_´n´, +R] {
311
-
defapply(x_1: T_1, ..., x_´n´:T_´n´):R
312
-
deftoString="<function>"
313
-
}
310
+
traitFunction´_n´[-´T_1´, ..., -´T_n´, +´R´]:
311
+
defapply(´x_1´: ´T_1´, ..., ´x_n´: ´T_n´): ´R´
312
+
overridedeftoString="<function´_n´>"
313
+
defcurried: ´T_1´ => ... => ´T_n´ =>R= ...
314
+
deftupled: ((´T_1´, ..., ´T_n´)) =>R= ...
315
+
```
316
+
317
+
For function types `Function´n´` where ´n > 22´, Scala defines a unique function class:
318
+
319
+
```scala
320
+
packagescala
321
+
traitFunctionXXL:
322
+
defapply(xs: IArray[Object]):Object
323
+
overridedeftoString="<functionXXL>"
314
324
```
315
325
326
+
There is no loss of type safety, as the internal representation is still `Function´n´` for all ´n´.
327
+
However this means methods `curried` and `tupled` are not available on functions with more than 22 parameters.
328
+
329
+
The implicitly imported [`Predef`](#the-predef-object) object defines the name
330
+
`Function` as an alias of `Function1`.
331
+
332
+
<!-- TODO: Remove below ? -->
316
333
The `PartialFunction` subclass of `Function1` represents functions that (indirectly) specify their domain.
317
334
Use the `isDefined` method to query whether the partial function is defined for a given input (i.e., whether the input is part of the function's domain).
The implicitly imported [`Predef`](#the-predef-object) object defines the name `Function` as an alias of `Function1`.
342
+
### Trait `Product`
343
+
<!-- TODO: Move somewhere else ? -->
344
+
<!-- TODO: Could not find more info on which non-Product methods case class automatically define -->
345
+
All case classes automatically extend the `Product` trait (and generate synthetic methods to conform to it) (but not `Product´n´`), and define a `_´n´` method for each of their arguments.
0 commit comments