The title might sound weird at beginning since many modern libraries or tools are designed to make developers write less code via ways like code generation or reflection. Those libraries are great and convenient to use because we write less code. However in my experience sometimes developers become careless and misuse those libraries by calling them everywhere they could. Eventually the codebase becomes difficult to read and maintain. Here are some examples I have seen many times and would like to share with you.
This is not something fancy or magical about Kotlin at all, but I was asked about this multiple times in the past few months, so I think this may be something worth to share.
The following code will not compile in Kotlin, and IDE shows an error: Smart cast to ‘String’ is impossible, because ‘name’ is a mutable property that could have been changed by this time:
If the warning message is already clear enough for you, you could stop reading right here :)
The rest of you might still wonder why the null check “does not work” or why…
In my previous post, Kotlin “By” Class Delegation: Favor Composition Over Inheritance, I covered using the
by keyword for class delegations. You can easily reuse and compose existing concrete implementations in your code. In this post, I will be going over using the same
by keyword for properties and how you can use them to create reusable code and reduce awkward boilerplate code. I will also include some Android specific samples with delegated properties.
JetBrains has a really detailed official documentation about Delegated Properties. …
KotlinConf is the first ever conference that is all about the new favorite language, Kotlin. The event is held by JetBrains at Pier 27, San Fransisco. It is two days long, and you can see many developers from big companies like Google, Gradle, Pivotal, and Facebook giving talks or attending events themselves. I was lucky enough that I purchased my ticket for $99 on the first day they announced the event, and four friends/coworkers of mine went together, making this event even more enjoyable.
When people ask me why I choose Kotlin over Java, I often say, “Because Kotlin is a better Java.” You get more than half of Effective Java implemented for you. In chapter 4 of the book, the author lists many items about inheritance because it is very error prone. The most well known item probably would be Item 16: Favor composition over inheritance. To implement that item, Kotlin introduces the special keyword
by to help you with implementing delegation in a single line.
Some side notes:
bycan be used with properties as well. …
A week ago I was thinking of writing a small Kotlin command line tool to read some local JSON files. I opened IntelliJ and started a Kotlin project with Gradle. Everything worked just fine. I could compile and run the output jar file until I imported autovalue. Autovalue uses an annotation processor to generate model objects, but IntelliJ just would not invoke annotation processing properly, so the compilation failed. I did some research and finally made it work, and I think these tricks are worth sharing.
I use gitlab and its built-in CI for my person blog (see details). Today I was trying to “update” hugo in the docker image, and I found myself making a couple of mistakes, including losing the dockerfile for the base image I built a long time ago! I decide to recreate a new one and push the
Dockerfile to public repo so that everyone can reuse it.
A few benefits of automated builds:
Genymotion is the most popular replacement for Android emulator, but by default it does not come with Google play services and play store. You will have to follow this guide and manually install one or two zip files to install required files. But now with version 2.10 Genymotion adds a one-click installer to automate the process!
Here’s the official release notes for the feature: link
Even though it’s a “1-click” installer, it took me 5 minutes to figure out where to “click”. So I took some screenshots for the installation. Hopefully these steps would help you as well.
Writing custom views or view groups is quite common nowadays, but sometimes it can be really cumbersome. You have to write overloaded constructors so that they would work properly in layout editor, or you define and implement some interfaces so that you don’t have to copy and paste the same code in different subclasses. With Kotlin we can easily avoid those common pain points when writing custom views and still enjoy full interoperability with Android!
Let’s get to the point straight. (This seems to be the norm recently). Kotlin provides:
@JvmOverloadsannotation to generate overloaded functions or constructors, which can…
Kotlin defines a few of extension functions like
apply() in its Standard.kt file. You probably have seen some of them in various tutorials or even used them already. Sometimes you may also wonder which one to use. In this post, I will walk through those confusing functions and see if we can understand the differences.
There are four (plus one added in 1.1) functions that look similar, and they are defined as follow:
* Calls the specified function [block] with `this` value as its receiver and returns its result.
public inline fun <T, R> T.run(block: T.() ->…