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/src/md/kotlin.core/annotations.md
+3Lines changed: 3 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,9 @@ An annotation type is a special kind of class type which is allowed to include r
17
17
- Other annotation types;
18
18
-[Arrays][Array types] of any type listed above.
19
19
20
+
> Important: when we say "other annotation types", we mean an annotation type cannot reference itself, either directly or indirectly.
21
+
> For example, if annotation type `A` references annotation type `B` which references an array of `A`, it is prohibited and reported as a compile-time error.
22
+
20
23
Annotation classes are not allowed to have any member functions, constructors or mutable properties.
21
24
They are also not allowed to have declared supertypes and are considered to be implicitly derived from `kotlin.Annotation`.
Copy file name to clipboardExpand all lines: docs/src/md/kotlin.core/declarations.md
+68-15Lines changed: 68 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -403,8 +403,8 @@ As such, data classes allow Kotlin to reduce the boilerplate and generate a numb
403
403
- `equals(that)` returns true iff:
404
404
- `that` has the same runtime type as `this`;
405
405
- `this.prop == that.prop` returns `true` for every data property `prop`;
406
-
- `hashCode()` returns the same numbers forobjects `A` and `B` if they are equal w.r.t. the generated `equals`;
407
-
- `toString` returns a string representations which is guaranteed to include the classname along with all the data properties' string representations.
406
+
- `hashCode()` returns the same numbers forvalues `A` and `B` if they are equal w.r.t. the generated `equals`;
407
+
- `toString()` returns a string representations which is guaranteed to include the classname along with all the data properties' string representations.
408
408
*A `copy()` function for shallow object copying with the following properties:
409
409
-It has the same number of parameters as the primary constructor with the same names and types;
410
410
-It calls the primary constructor with the corresponding parameters at the corresponding positions;
@@ -499,6 +499,35 @@ Data classes have the following restrictions:
499
499
>
500
500
>Disclaimer: the implementations of these methods given inthis examples are not guaranteed to exactly match the ones generated by kotlin compiler, please refer to the descriptions of these methods above for guarantees
501
501
502
+
##### Dataobject declaration
503
+
504
+
>Note:as of Kotlin $\currentKotlinMajorVersion{}$, this feature is experimental.
505
+
506
+
A data object $\dataObject$ is a special kind of [object][Object declaration], which extends the [data class][Dataclassdeclaration] abstraction (product type of one or more data properties) to a case of unit type: product type of zero data properties.
507
+
508
+
>Note: unit type has only one possible value, thus it is also known as singleton type.
509
+
510
+
Similarly to data classes, there are a number of functions with predefined behaviour generated for data objects.
- `equals(that)` returns true iff `that` has the same runtime type as `this`;
514
+
- `hashCode()` returns the same numbers for values `A` and `B` if they are equal w.r.t. the generated `equals`;
515
+
- `toString()` returns a string representations which is guaranteed to include the object name.
516
+
517
+
>Note: `copy()` and `componentN()` functions are not generated, as they are not relevant for a unit type.
518
+
>
519
+
>* `copy()` function isnot needed as unit type has a single possible value;
520
+
>* `componentN()` functions are not needed as unit type has no data properties.
521
+
522
+
Unlike data classes, however, for data objects the only generated function which can be exemplified or inherited is `toString()`; `equals()` and `hashCode()` for a data objectalways work as specified above.
523
+
Thisis to ensure data objects donot violate the unit type invariant of "being inhabited by only one value", which would be possible if one were to provide a custom `equals()` implementation.
524
+
525
+
If either `equals()` or `hashCode()` function would be exemplified or inherited by a data object, it is a compile-time error.
526
+
527
+
Data objects have the same restrictions are regular [objects][Object declaration].
528
+
529
+
>Note: [companion objects][Class declaration] and [object literals][Object literals] cannot be data objects.
530
+
502
531
#### Enumclassdeclaration
503
532
504
533
Enumclass $E$ is a special kind of classwith the following properties:
@@ -510,6 +539,8 @@ Enum class $E$ is a special kind of class with the following properties:
510
539
-It cannot have type parameters of any kind;
511
540
-It has special syntax to accommodate for the properties described above.
512
541
542
+
>Note:for the purposes of overload resolution, enum entries are considered to be [static member callables][Call with an explicit type receiver] of the enumclasstype
543
+
513
544
Enumclassbody uses special kind of syntax (see grammar) to declare enum entries in addition to all other declarations inside the classbody.
514
545
Enum entries have their own bodies that may contain their own declarations, similar to [object declarations][Classifier declaration].
515
546
@@ -542,27 +573,41 @@ Every enum entry of class `E` implicitly overrides members of `kotlin.Enum<E>` i
542
573
543
574
(a member of `kotlin.Any`) defined by default to return the entry name, but may be overridden to have different behaviour both in the enumclassdeclaration and in entry declarations.
544
575
545
-
In addition to these, every enumclasstype `E` has the following **static** member functions declared implicitly:
576
+
In addition to these, every enumclasstype `E` has the following **static** members declared implicitly:
577
+
578
+
- ```kotlin
579
+
publicfinal static val entries:EnumEntries<E>
580
+
```
581
+
582
+
This property returns an instance of a special immutable `EnumEntries<E>` list of all possible enum values in the order they are declared;
546
583
547
584
- ```kotlin
548
585
publicfinal static funvalueOf(value:String): E
549
586
```
550
587
551
-
returning an object corresponding to the entry with the name equal to `value` parameter of the call or throws an exception otherwise;
588
+
This function returns an object corresponding to the entry with the name equal to `value` parameter of the call or throws an exception otherwise.
589
+
590
+
>Important: `static` isnot a valid Kotlin keyword andis only used here for clarity.
591
+
>The static members are handled differently by the [overload resolution][Call with an explicit type receiver].
592
+
593
+
Kotlin standard library also introduces a function to access all enum values for a specific enumclasscalled `kotlin.enumEntries<T>`.
594
+
Please refer to the standard library documentation for details.
595
+
596
+
>Note: the `entries` property is available since Kotlin1.9.
597
+
598
+
For backwards compatibility, in addition to the `entries` property, every enumclasstype `E` has the following **static** member function declared implicitly.
552
599
553
600
- ```kotlin
554
601
publicfinal static funvalues(): kotlin.Array<E>
555
602
```
556
-
557
-
returning an [array][Array types] of all possible enum values in the order they are declared.
558
-
Every invocation of this function returns a new array to disallow changing its contents.
559
603
560
-
>Important: `static` isnot a valid Kotlin keyword andis only used here for clarity
604
+
This function returns an [array][Array types] of all possible enum values in the order they are declared.
605
+
Every invocation of this function returns a new array to disallow changing its contents.
561
606
562
-
>Note: these static member functions are handled differently by the [overload resolution][Overload resolution].
607
+
>Important: `values` function is effectively deprecated and `entries` property should be used instead.
563
608
564
-
>Note:Kotlin standard library introduces another function to access all enum values for a specific enumclasscalled `kotlin.enumValues<T>`.
565
-
>Please refer to the standard library documentation for details.
609
+
Kotlin standard library also introduces another function to access all enum values for a specific enumclasscalled `kotlin.enumValues<T>` (which is deprecated for subsequent removal).
610
+
Please refer to the standard library documentation for details.
566
611
567
612
>Example:
568
613
>
@@ -622,6 +667,9 @@ Annotation classes have the following properties:
622
667
-Otherannotation types;
623
668
-Arrays of any other allowed type.
624
669
670
+
>Important:when we say "other annotation types", we mean an annotation type cannot reference itself, either directly or indirectly.
671
+
>For example, ifannotation type `A` references annotation type `B` which references an array of `A`, it is prohibited and reported as a compile-time error.
672
+
625
673
>Note:annotation classes can have type parameters, but cannot use them as types for their primary constructor parameters.
626
674
>Their main use isfor various annotation processing tools, which can access the type arguments from the source code.
627
675
@@ -796,6 +844,8 @@ Similarly to interfaces, we shall specify object declarations by highlighting th
796
844
> Note: this section is about declaration of _named_ objects.
797
845
> Kotlin also has a concept of _anonymous_ objects, or object literals, which are similar to their named counterparts, but are expressions rather than declarations and, as such, are described in the [corresponding section][Object literals].
A class (but not an interface or an object) may be declared *locally* inside a [statement scope][Scopes and identifiers] (namely, inside a function).
@@ -1130,6 +1180,8 @@ They may also be passed to other functions as `noinline` or `crossinline` argume
1130
1180
1131
1181
Particular platforms may introduce additional restrictions or guarantees for the inlining mechanism.
1132
1182
1183
+
>Important:for extension functions, the extension receiver is considered to be effectively `noinline`.
1184
+
1133
1185
>Examples:
1134
1186
>
1135
1187
> ```kotlin
@@ -1385,7 +1437,9 @@ Properties without backing fields are not allowed to have initializer expression
1385
1437
1386
1438
Read/write access to the property is replaced with getter/setter invocation respectively.
1387
1439
Getters and setters allow for some modifiers available for function declarations (for example, they may be declared `inline`, see grammar for details).
1440
+
1388
1441
Properties themselves may also be declared `inline`, meaning that both getter and setter of said property are `inline`.
1442
+
Additionally, `inline` properties are not allowed to have backing fields, i.e., they must have custom accessors which do not use the `field` property.
1389
1443
1390
1444
#### Delegated property declaration
1391
1445
@@ -1706,7 +1760,6 @@ The following declarations are not allowed to have type parameters:
1706
1760
- Constructor declarations;
1707
1761
- Getters and setters of property declarations;
1708
1762
- Enum class declarations;
1709
-
- Annotation class declarations;
1710
1763
- Classifier declarations inheriting from `kotlin.Throwable`.
1711
1764
1712
1765
Type parameters are allowed to specify *subtyping restrictions* on them in the form `T : U`, meaning $T <: U$ where $T$ is a type parameter and $U$ is some other type available in the scope the declaration is declared in.
@@ -1791,9 +1844,9 @@ By supplying this annotation the author of the code explicitly declares that saf
1791
1844
1792
1845
#### Reified type parameters
1793
1846
1794
-
Type parameters of inline function declarations (and only those) can be declared `reified` using the corresponding keyword.
1795
-
A reified type parameter is a [runtime-available][Runtime-available types] type inside the function scope, see the corresponding section for details.
1796
-
Reified type parameters can only be substituted by other [runtime-available types][Runtime-available types] when using such functions.
1847
+
Type parameters of inline function or property declarations (and only those) can be declared `reified` using the corresponding keyword.
1848
+
A reified type parameter is a [runtime-available][Runtime-available types] type inside their declaration's scope, see the corresponding section for details.
1849
+
Reified type parameters can only be substituted by other [runtime-available types][Runtime-available types] when using such declarations.
Copy file name to clipboardExpand all lines: docs/src/md/kotlin.core/expressions.md
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -418,7 +418,7 @@ If when expression is not [exhaustive][Exhaustive when expressions], it has type
418
418
> }
419
419
> ```
420
420
421
-
When with bound value also allows for an inline property declaration of the form `when (valV=E) { ... }` inside the parentheses.
421
+
When with bound value also allows for an in-place property declaration of the form `when (valV=E) { ... }` inside the parentheses.
422
422
This declares a new property (see [declaration sections][Property declaration] for details) alongside the usual mechanics of the *when-expression*.
423
423
The scope of this property is limited to the `when` expression, including both conditions and control structure bodies of the expression.
424
424
As its form is limited to a simple "assignment-like" declaration with an initializer, this property does not allow getters, setters, delegation or destructuring.
@@ -711,15 +711,16 @@ The type of elvis operator expression is the [least upper bound][Least upper bou
711
711
:::{.paste target=grammar-rule-rangeExpression}
712
712
:::
713
713
714
-
A*range expression*is a binary expression which uses a range operator `..`.
715
-
Itis an [overloadable][Operator overloading] operator with the following expansion:
714
+
A*range expression*is a binary expression which uses a range operator `..`or a range-until operator `..<`.
715
+
These are [overloadable][Operator overloading] operators with the following expansions:
716
716
717
717
- `A..B` is exactly the same as `A.rangeTo(B)`
718
+
- `A..<B` is exactly the same as `A.rangeUntil(B)`
718
719
719
-
where `rangeTo` is a valid operator function available in the current scope.
720
+
where `rangeTo` or `rangeUntil` is a valid operator function available in the current scope.
720
721
721
-
Thereturn type of this functionisnot restricted.
722
-
A range expression has the same type as the return type of the corresponding `rangeTo` overload variant.
722
+
The return type of these functions is not restricted.
723
+
A range expression has the same type as the return type of the corresponding operator function overload variant.
Copy file name to clipboardExpand all lines: docs/src/md/kotlin.core/overload-resolution.md
+10-1Lines changed: 10 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -230,7 +230,8 @@ They mostly follow the same rules as [calls with an explicit value receiver][Cal
230
230
However, for a callable `f` with an explicit type receiver `T` the following sets are analyzed (**in the given order**):
231
231
232
232
1. Static member callables named `f` of type `T`;
233
-
2. The overload candidate sets for call `T.f()`, where `T` is a companionobject of type `T`.
233
+
2. Static member callables named `f` of type `T` declared implicitly;
234
+
3. The overload candidate sets for call `T.f()`, where `T` is a companionobject of type `T`.
234
235
235
236
##### Call with an explicit super-form receiver
236
237
@@ -730,6 +731,14 @@ TODO: more examples
730
731
731
732
> Note: this is different from the overload resolution for regular calls in that no most specific candidate selection process is performed inside the sets
732
733
734
+
> Important: when the callable reference resolution for `T::f` requires building overload candidate sets for both [type][Call with an explicit type receiver] and [value][Call with an explicit receiver] receiver candidates, they are considered in the following order.
735
+
>
736
+
> 1. Static member callables named `f` of type `T`;
737
+
> 2. The overload candidate sets for call `t::f`, where `t` is a value of type `T`;
738
+
> 3. The overload candidate sets for call `T::f`, where `T` is a companion object of type `T`.
739
+
>
740
+
> Callable references to members of companion objects are deprioritized, as you could always use the `T.Companion::f` syntax to reference them.
741
+
733
742
> Important: when building the OCS for a callable reference, `invoke` operator convention does not apply, and all property references are treated equally as function references, being placed in the same sets.
0 commit comments