官术网_书友最值得收藏!

Concise yet expressive code

While being interoperable, Kotlin code is far superior to Java code. Like Scala, Kotlin uses type inference to cut down on a lot of boilerplate code and makes it concise. (Type inference is a better feature than dynamic typing as it reduces the code without sacrificing the robustness of the end product). However, unlike Scala, Kotlin code is easy to read and understand, even for someone who may not know Kotlin.

Kotlin's data class construct is the most prominent example of being concise as shown in the following:

    data class Employee (val id: Long, var name: String) 

Compared to its Java counterpart, the preceding line has packed into it the class definition, member variables, constructor, getter-setter methods, and also the utility methods, such as equals() and hashCode(). This will easily take 15-20 lines of Java code.

The data classes construct is not an isolated example. There are many others where the syntax is concise and expressive. Consider the following as additional examples:

  • Kotlin's default values to function parameters save the need to overload the functions
  • Kotlin's extension functions can be used to add domain-specific functionality to existing classes, making it easy for someone from the domain to understand
主站蜘蛛池模板: 鄂州市| 沙坪坝区| 新乡县| 广宗县| 肥城市| 长沙县| 年辖:市辖区| 灵川县| 梁平县| 屏山县| 丘北县| 界首市| 牙克石市| 棋牌| 贞丰县| 桃源县| 北安市| 洛浦县| 清新县| 梅州市| 江阴市| 山阳县| 松江区| 易门县| 牡丹江市| 武功县| 屯留县| 周至县| 清徐县| 丹江口市| 阳高县| 九龙坡区| 英吉沙县| 永丰县| 中山市| 高陵县| 永寿县| 乌什县| 芷江| 台北县| 达日县|