However, there are some differences between the two concepts. Read-only collections are similar to immutable collections in that they both provide collections that cannot be modified once they are created. It is a common mistake in Kotlin to confuse read-only collections with immutable collections. Using any of the mutableOf() functions will create a mutable collection of that type. listOf() or mapOf()) you’ll get a read-only instance of that collection type. If you use any of the Of() functions (e.g. ImmutabilityĪs already mentioned above, Kotlin has read-only collections. Don’t use withDefault, use getOrDefault when you need to rely on a fallback value. This program will actually output default as we intended (because the key "2" is missing). What do you expect the outcome of this program to be? fun main (args: Array). Let’s start off with a rather simple “hello world” example. In this post I’ll show you 7 code snippets containing common mistakes (I made) when programming in Kotlin. Or maybe even worse: I can’t explain why the code DOES work. And even now I have some experience, every once in a while, when conducting a Kotlin training or when on a Kotlin assignment, I encounter situations where I simply cannot explain why the code doesn’t work the way I intended. Have you ever felt like this kid when programming? I sure did! When I got into Kotlin development I felt like this kid many many times. This article was originally published at my personal blog on March 9, 2023.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |