Skip to content

Commit 339ea47

Browse files
committed
fixup! Update KEEP on Explicit backing fields feature
1 parent ebd432b commit 339ea47

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

proposals/explicit-backing-fields.md

+28
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class SomeViewModel : ViewModel() {
5555
* [Grammar changes](#grammar-changes)
5656
* [Java interoperability](#java-interoperability)
5757
* [Reflection](#reflection)
58+
* [Risks](#risks)
59+
* [Unintentionally exposing a field as a return value](#unintentionally-exposing-a-field-as-a-return-value)
5860
* [Future enhancements](#future-enhancements)
5961
* [Underscore operator in type parameters](#underscore-operator-in-type-parameters)
6062

@@ -448,6 +450,32 @@ On JVM backing field can be obtained using [`javaField` extension](https://kotli
448450
There is no API in Reflection to check whether property has an explicit backing field or obtain any information about it.
449451
Adding such an API might be considered in [Future enhancements](#future-enhancements).
450452

453+
## Risks
454+
455+
### Unintentionally exposing a field as a return value
456+
457+
The ability to call both a getter and a field with the same name depending on the context of call
458+
introduces a possible error with the unwanted exposing field value (where getter value was meant),
459+
when property is returned in function, especially when function return type is omitted:
460+
461+
```kotlin
462+
class MyViewModel {
463+
val city = field.asStateFlow()
464+
field = MutableStateFlow("")
465+
466+
fun updateAndReturn(newValue: String): StateFlow<String> {
467+
city.value = newValue
468+
return city // exposed MutableStateFlow instead of read-only wrapper
469+
}
470+
471+
fun updateAndReturn2(newValue: String) /* MutableStateFlow inferred */ =
472+
city.also { it.value = newValue }
473+
}
474+
```
475+
476+
Although it would be nice to have warnings from the tooling in such cases,
477+
it is unclear in which cases such behavior is desirable and in which it is not.
478+
451479
## Future enhancements
452480

453481
We strive to keep the design simple and uncluttered, so at this point we put aside functionality, which is rarely used or would complicate language too much.

0 commit comments

Comments
 (0)