![]() ![]() What happened above is that the lambda result from let was null so a third value was used, neither non-empty nor null, because the way the code was structured allowed for having three branches in the code. ![]() This would be similar to having an if-else statement where both the if and else can be executed in a single run, which is definitely not the correct behavior most of us are expecting from an if-else statement.Īs a practical example, let’s consider the following code:Īs we can see, even when string isn’t null we might not necessarily get the value returned by let if that value is actually null. One such example is something I’ve come to see more and more with time: using a?.let as well as the right-side of the Elvis operator. On the other hand, Kotlin is a fairly new language, it has about 10 years, needing some more time to mature, which brings along some dangers because usage of particular constructs can have its perils, without us even realizing. Its use of functional constructs provides developers with power and flexibility that goes way beyond some languages out there - I had been programming in Python and Go previously. Now, Kotlin is a very powerful language and I’ve been enjoying it more and more. This time I want to write about a pattern I’ve been thinking about: abusing scope functions, in particular let, and using them as if-else for null checks. I’ve been trying to understand more about Kotlin and I’ve written about Kotlin topics more lately, such using require and check functions in Kotlin. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |