v1.1.0-beta1: mini function declarations and working properties
This commit is contained in:
1 parent
17b4904ae3
commit
8300c2a3ab
5 files changed
+39
-7
No files matched your search
+6
-1
@@ -74,14 +74,18 @@ assertEquals("Adult", p.ageCategory)
|
||||
|
||||
### Laconic Expression Shorthand
|
||||
|
||||
For simple accessors, you can use the `=` shorthand for a more elegant and laconic form:
|
||||
For simple accessors and methods, you can use the `=` shorthand for a more elegant and laconic form:
|
||||
|
||||
```kotlin
|
||||
class Circle(val radius: Real) {
|
||||
val area get() = π * radius * radius
|
||||
val circumference get() = 2 * π * radius
|
||||
|
||||
fun diameter() = radius * 2
|
||||
}
|
||||
|
||||
fun median(a, b) = (a + b) / 2
|
||||
|
||||
class Counter {
|
||||
private var _count = 0
|
||||
var count get() = _count set(v) = _count = v
|
||||
@@ -92,6 +96,7 @@ class Counter {
|
||||
|
||||
- **`val` properties** must have a `get()` accessor and cannot have a `set()`.
|
||||
- **`var` properties** must have both `get()` and `set()` accessors.
|
||||
- **Functions and methods** can use the `=` shorthand to return the result of a single expression.
|
||||
- **No Backing Fields**: There is no magic `field` identifier. If you need to store state, you must declare a separate (usually `private`) field.
|
||||
- **Type Inference**: You can omit the type declaration if it can be inferred or if you don't need strict typing.
|
||||
|
||||
|
||||
Reference in new issue
Block a user