Skip to content

Commit deba8dc

Browse files
committed
fix: SME review
1 parent bb18630 commit deba8dc

File tree

4 files changed

+56
-35
lines changed

4 files changed

+56
-35
lines changed

docs/topics/functions.md

Lines changed: 53 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ you can move parameters within the declaration without worrying about which is g
5555
Kotlin functions can receive other functions as parameters — and be passed as arguments.
5656
For details, see [](lambdas.md).
5757

58-
### Parameters with default values (optional parameters)
58+
### Parameters with default values (optional parameters) {id="parameters-with-default-values"}
5959

6060
Function parameters can have default values, which are used when you skip the corresponding argument.
6161
This reduces the number of necessary overloads.
@@ -71,8 +71,8 @@ fun read(
7171
) { /*...*/ }
7272
```
7373

74-
If a parameter with default value precedes a parameter with no default value, the default value can only be used by calling
75-
the function with [named arguments](#named-arguments):
74+
When you declare a parameter with a default value before a parameter without a default value,
75+
you can only use the default value by [naming arguments](#named-arguments):
7676

7777
```kotlin
7878
fun foo(
@@ -84,6 +84,18 @@ foo(bar = 1) // Uses the default value foo = 0
8484
foo(1) // Error: No value passed for parameter 'bar'
8585
```
8686

87+
[Trailing lambdas](lambdas.md#passing-trailing-lambdas) are an exception to this rule,
88+
since the last parameter must correspond to the passed function:
89+
90+
```kotlin
91+
fun foo(
92+
foo: Int = 0,
93+
bar: () -> Unit,
94+
) { bar() }
95+
96+
foo() { println ("bar") } // Uses the default value for 'foo' and prints "bar"
97+
```
98+
8799
[Overriding methods](inheritance.md#overriding-methods) always use the base method's default parameter values.
88100
When overriding a method that has default parameter values, the default parameter values must be omitted from the signature:
89101

@@ -213,16 +225,41 @@ foo(strings = arrayOf("a", "b", "c"))
213225
>
214226
{style="note"}
215227

216-
### Unit-returning functions
228+
### Explicit return types
229+
230+
Functions with a block body must always specify return types explicitly, unless it's intended for them to return `Unit`,
231+
[in which case specifying the return type is optional](#unit-returning-functions).
217232

218-
If a function does not return a useful value, its return type is `Unit` (corresponds to the `void` type in Java).
233+
Kotlin does not infer return types for functions with block bodies because such functions may have complex control flow
234+
in the body, and the return type will be non-obvious to the reader (and sometimes even for the compiler).
235+
But the return type is inferred, unless specified, for [single-expression functions](#single-expression-functions).
236+
237+
### Single-expression functions
238+
239+
When the function body consists of a single expression, the curly braces can be omitted and the body specified after an `=` symbol:
240+
241+
```kotlin
242+
fun double(x: Int): Int = x * 2
243+
```
244+
245+
Explicitly declaring the return type is [optional](#explicit-return-types) when this can be inferred by the compiler:
246+
247+
```kotlin
248+
fun double(x: Int) = x * 2
249+
```
250+
251+
### Unit-returning functions
219252

253+
If a function has a block body and does not return a useful value, its return type is assumed to be `Unit` (corresponds to the `void` type in Java).
220254
`Unit` is a type with only one value - `Unit`.
221-
You don't have to declare this return type, or return `Unit` explicitly.
255+
256+
You don't have to specify `Unit` as a return type, except for functional type parameters,
257+
and you never have to return `Unit` explicitly.
258+
222259
Therefore, this verbose declaration:
223260

224261
```kotlin
225-
fun printHello(name: String?): Unit {
262+
fun printHello(name: String?, aux: () -> Unit): Unit {
226263
if (name != null)
227264
println("Hello $name")
228265
else
@@ -231,20 +268,18 @@ fun printHello(name: String?): Unit {
231268
}
232269
```
233270

234-
is equivalent to:
271+
can be shortened to:
235272

236273
```kotlin
237-
fun printHello(name: String?) { /*...*/ }
274+
// The declaration of 'aux' still needs an explicit return type
275+
fun printHello(name: String?, aux: () -> Unit) {
276+
if (name != null)
277+
println("Hello $name")
278+
else
279+
println("Hi there!")
280+
}
238281
```
239282

240-
### Explicit return types
241-
242-
Functions with block body must always specify return types explicitly, unless it's intended for them to return `Unit`,
243-
[in which case specifying the return type is optional](#unit-returning-functions).
244-
245-
Kotlin does not infer return types for functions with block bodies because such functions may have complex control flow
246-
in the body, and the return type will be non-obvious to the reader (and sometimes even for the compiler).
247-
248283
### Variable number of arguments (varargs)
249284

250285
You can mark a parameter of a function (usually the last one) with the `vararg` modifier:
@@ -287,20 +322,6 @@ val a = intArrayOf(1, 2, 3) // IntArray is a primitive type array
287322
val list = asList(-1, 0, *a.toTypedArray(), 4)
288323
```
289324

290-
### Single-expression functions
291-
292-
When the function body consists of a single expression, the curly braces can be omitted and the body specified after an `=` symbol:
293-
294-
```kotlin
295-
fun double(x: Int): Int = x * 2
296-
```
297-
298-
Explicitly declaring the return type is [optional](#explicit-return-types) when this can be inferred by the compiler:
299-
300-
```kotlin
301-
fun double(x: Int) = x * 2
302-
```
303-
304325
### Infix notation
305326

306327
Functions marked with the `infix` keyword can also be called using the infix notation (omitting the dot and the parentheses
@@ -309,7 +330,7 @@ for the call). Infix functions must meet the following requirements:
309330
* They must be member functions or [extension functions](extensions.md).
310331
* They must have a single parameter.
311332
* The parameter must not [accept variable number of arguments](#variable-number-of-arguments-varargs) and must have
312-
no [default value](#parameters-with-default-values-optional-parameters).
333+
no [default value](#parameters-with-default-values).
313334

314335
```kotlin
315336
infix fun Int.shl(x: Int): Int { /*...*/ }

docs/topics/jvm/comparison-to-java.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Kotlin fixes a series of issues that Java suffers from:
4545
* [Data classes](data-classes.md)
4646
* [Coroutines](coroutines-overview.md)
4747
* [Top-level functions](functions.md)
48-
* [Parameters with default values](functions.md#parameters-with-default-values-optional-parameters)
48+
* [Parameters with default values](functions.md#parameters-with-default-values)
4949
* [Named parameters](functions.md#named-arguments)
5050
* [Infix functions](functions.md#infix-notation)
5151
* [Expect and actual declarations](https://kotlinlang.org/docs/multiplatform/multiplatform-expect-actual.html)

docs/topics/jvm/java-to-kotlin-nullability-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ findOrder()?.customer?.let(::processCustomer)
218218

219219
## Default values instead of null
220220

221-
Checking for `null` is often used in combination with [setting the default value](functions.md#parameters-with-default-values-optional-parameters)
221+
Checking for `null` is often used in combination with [setting the default value](functions.md#parameters-with-default-values)
222222
in case the null check is successful.
223223

224224
The Java code with a null check:

docs/topics/keyword-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ Kotlin supports the following operators and special symbols:
135135
- `*` is also used to [pass an array to a vararg parameter](functions.md#variable-number-of-arguments-varargs).
136136
* `=`
137137
- assignment operator.
138-
- is used to specify [default values for parameters](functions.md#parameters-with-default-values-optional-parameters).
138+
- is used to specify [default values for parameters](functions.md#parameters-with-default-values).
139139
* `+=`, `-=`, `*=`, `/=`, `%=` - [augmented assignment operators](operator-overloading.md#augmented-assignments).
140140
* `++`, `--` - [increment and decrement operators](operator-overloading.md#increments-and-decrements).
141141
* `&&`, `||`, `!` - logical 'and', 'or', 'not' operators (for bitwise operations, use the corresponding [infix functions](numbers.md#operations-on-numbers) instead).

0 commit comments

Comments
 (0)