Why is this not allowed? ``` val map: Map&...
# announcements
m
Why is this not allowed?
Copy code
val map: Map<String, Int>? = mutableMapOf("one" to 3)
        map?.["one"] // <- Why is this not allowed?
        map?.get("one") // <- Which is equivalent to this, which works
s
Use map?["one"] Without the dot
👎 2
m
Still doesn’t work, here is Kotlin Playground for it: https://pl.kotl.in/IxeuOxPo6
m
That doesn't compile
You can do
map?.let { it["one"] }
. The
?
just doesn't work with the operator syntax.
r
There is a long standing issue for it (unfortunately it doesn't look like it has any traction): https://youtrack.jetbrains.com/issue/KT-8799
1
a
You can't use this access syntax [] on a nullable receiver either for map or array only ?.get
The IDE actually should have a warning for that and autocorrect at least Android Studio has