Skip to content

Commit 79982fd

Browse files
Merge pull request Kotlin#49 from p7nov/productivity-revised
docs: revised Productivity Boosters
2 parents ce9e9dc + 40a01fb commit 79982fd

File tree

5 files changed

+32
-33
lines changed

5 files changed

+32
-33
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# Productivity boosters
1+
# Productivity Boosters
22

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Named Arguments
22

3+
As with most other programming languages (Java, C++, etc.), Kotlin supports passing arguments to methods and constructors according to the order they are defined.
4+
Kotlin also supports [named arguments](https://kotlinlang.org/docs/reference/functions.html#named-arguments) to allow clearer invocations and avoid mistakes with the order of arguments. Such mistakes are hard to find because they are not detected by the compiler, for example, when two sequential arguments have the same type.
35
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
46

57
```kotlin
@@ -8,19 +10,16 @@ fun format(userName: String, domain: String) = "$userName@$domain"
810
fun main() {
911
//sampleStart
1012
println(format("mario", "example.com")) // 1
11-
println(format(userName = "foo", domain = "bar.com")) // 2
12-
println(format(domain = "frog.com", userName = "pepe")) // 3
13+
println(format("domain.com", "username")) // 2
14+
println(format(userName = "foo", domain = "bar.com")) // 3
15+
println(format(domain = "frog.com", userName = "pepe")) // 4
1316
//sampleEnd
1417
}
1518
```
1619

1720
</div>
1821

19-
1. As most of programming languages(Java, C++ etc), Kotlin supports passing arguments to methods and constructors
20-
according to their order of definition.
21-
2. Kotlin also supports named arguments to allow clearer invocations and avoid
22-
mistakes that otherwise would not be detected by the compiler (e.g.
23-
switching the correct order of the arguments and constructing an email in
24-
the form _gmail.com@username_).
25-
3. When invoking a method or constructor, named arguments do not have to
26-
respect the original order of definition.
22+
1. Calls a function with argument values.
23+
2. Calls a function with switched arguments. No syntax errors, but the result _domain.com@username_ is incorrect.
24+
3. Calls a function with named arguments.
25+
4. When invoking a function with named arguments, you can specify them in any order you like.

examples/06_productivity_boosters/02_String Templates.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# String Templates
22

3+
[String templates](https://kotlinlang.org/docs/reference/basic-types.html#string-templates) allow you to include variable references and expressions into strings. When the value of a string is requested (for example, by `println`), all references and expressions are substituted with actual values.
34
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
45

56
```kotlin
@@ -15,6 +16,6 @@ fun main() {
1516

1617
</div>
1718

18-
1. *Strings* in Kotlin can include references to variables that are interpolated
19-
2. In addition to simple variable references, they can also include any expression enclosed in curly braces.
19+
1. Prints a string with a variable reference. References in strings start with `$`.
20+
2. Prints a string with an expression. Expressions start with `$` and are enclosed in curly braces.
2021

examples/06_productivity_boosters/03_Destructuring Declarations.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Destructuring Declarations
22

3-
_Destructuring declaration_ syntax can be very handy and save you few lines of code.
3+
[Destructuring declaration](https://kotlinlang.org/docs/reference/multi-declarations.html#destructuring-declarations) syntax can be very handy, especially when you need an instance only for accessing its members. It lets you define the instance without a specific name therefore saving a few lines of code.
44

55
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
66

@@ -27,9 +27,9 @@ fun main() {
2727

2828
</div>
2929

30-
1. Create a component on the left-hand side to match the arity of the right hand
31-
2. It can be used to iterate through maps. _name_ and _age_ variables are mapped to key and value now
32-
3. You can destructure built-in Pairs and Triples, even as return values from functions
30+
1. Destructures an `Array`. The number of variables on the left side matches the number of arguments on the right side.
31+
2. Maps can be destructured as well. `name` and `age` variables are mapped to the map key and value.
32+
3. Built-in `Pair` and `Triple` types support destructuring too, even as return values from functions.
3333

3434
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
3535

@@ -50,10 +50,10 @@ fun main() {
5050

5151
</div>
5252

53-
1. Define a data class that will be destructured later
54-
2. Values mapped to object fields
55-
3. Data class automatically defines the `component1()`, `component2()` corresponding methods, which will be called during destructuring
56-
4. Use _underscore_ if you don't need one of the values, avoiding the compiler hint indicating unused variable
53+
1. Defines a data class.
54+
2. Destructures an instance. Declared values are mapped to the instance fields.
55+
3. Data class automatically defines the `component1()` and `component2()` methods that will be called during destructuring.
56+
4. Use _underscore_ if you don't need one of the values, avoiding the compiler hint indicating an unused variable.
5757

5858
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
5959

@@ -77,5 +77,5 @@ fun main() {
7777

7878
</div>
7979

80-
1. Define a custom `Pair` class with `component1()` and `component2()` methods
81-
2. Destructure instances of this class same as built-in `Pair`
80+
1. Defines a custom `Pair` class with `component1()` and `component2()` methods.
81+
2. Destructures an instance of this class the same way as for built-in `Pair`.

examples/06_productivity_boosters/04_Smart Casts.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Smart Casts
22

33

4-
The Kotlin compiler is smart enough to perform type casts automatically in most cases, including
5-
1. Casts from nullable types to their non-nullable counterparts
6-
2. Casts from a supertype to a subtype
4+
The Kotlin compiler is smart enough to perform type casts automatically in most cases, including:
5+
1. Casts from nullable types to their non-nullable counterparts.
6+
2. Casts from a supertype to a subtype.
77

88
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
99

@@ -38,11 +38,10 @@ fun main() {
3838

3939
</div>
4040

41+
1. Declares a nullable variable.
42+
2. Smart-cast to non-nullable (thus allowing direct access to `isLeapYear`).
43+
3. Smart-cast inside a condition (this is possible because, like Java, Kotlin uses [short-circuiting](https://en.wikipedia.org/wiki/Short-circuit_evaluation)).
44+
4. Smart-cast inside acondition (also enabled by short-circuiting).
45+
5. Smart-cast to the subtype `LocalDate`.
4146

42-
1. Declares a nullable variable
43-
2. Smart-cast to non-nullable (thus allowing direct access to `isLeapYear`)
44-
3. Smart-cast inside condition (this is possible because, like Java, Kotlin uses [short-circuiting](https://en.wikipedia.org/wiki/Short-circuit_evaluation))
45-
4. Smart-cast inside condition (also enabled by short-circuiting)
46-
5. Smart-cast to subtype LocalDate
47-
48-
This way, you can automatically use variables as desired most of the time without doing obvious casts manually.
47+
This way, you can automatically use variables as desired in most cases without doing obvious casts manually.

0 commit comments

Comments
 (0)