Kotlin vs Scala

Comparing of Java Virtual Machine - compatible programming languages
28 August 2017   2438

Java is one of the most popular languages ​​for business applications development. However, because of its enterprise-oriented approach, it is evolving extremely slow, which is not satisfied by all programmers. Therefore, periodically there are new JVM-compatible languages ​​that are designed to solve Java problems.

Two of them are Kotlin and Scala. The first is a relatively new development from JetBrains, fully compatible with the standard Java code and significantly expanding its functionality. Scala represents the symbiosis of Java and C #, combining object-oriented programming with functional.

Let's have a good look at these languages ​​in more detail and understand what their key differences are and how they affect the scope of these development tools.

Complexity of studying

Due to the fact that both languages ​​are based on the Java-based business development framework, the basic threshold for entry into them is quite large. Therefore, it is more logical to compare them in the perspective of a developer already familiar with Java.

Kotlin is extremely similar to his "parent". A Java programmer will easily understand most of the Kotlin code without any specific knowledges. The mastering and learning of additional possibilities like lambda functions also does not take much time.

val numbers = 1..10
val doubles = numbers.map { it * 2 }
val sumOfSquares = doubles.fold(0) {x,y -> x+y }

Scala is a more complex language. It introduces quite a lot of innovations in comparison with Java and serves rather than its "expansion" as Kotlin, but a powerful tool for developers who feel that Java limits them. Accordingly, a comprehensive study of Scala will take a long time.

val numbers = 1 to 10
val doubles = numbers.map { _ * 2 }
val sumOfSquares = doubles.fold(0) { _ + _ }

Programming approach

Kotlin, although it has some signs of a functional language, still gravitates toward object-oriented paradigms. This is a definite plus for programmers, who previously worked only with OOP, but developers who are accustomed to functional programming may not have enough capabilities.

Scala offers great flexibility. When writing code in this language, you can take full advantage of both the object-oriented and functional approach.

Features

  • Type declaration. Both languages ​​are statically typed. Variable type declaration is optional.
  • Null Safely. Scala uses an option monad for this purpose, which can exist in one of two states: Some (x) or None. In Kotlin for error protection, new types of nullable and non-null variables have been added, to which null can not be written. In general, the Kotlin approach makes the code more readable and concise.
  • Classes of data. Scala uses case classes that support methods for creating new instances and declaring properties. Kotlin offers a similar set of features in general, but its classes are more suitable for working with Java-frameworks, because it looks like JavaBean.
  • Scala has problems with the syntax of the infix and postfix operators. Kotlin is a slightly more formalized language, which improves the readability of the code and removes the excessive ambiguity of the syntax.
  • Implicit conversions in Scala can significantly change the behavior of the code after importing - it is difficult to determine exactly which variable will be passed to the function. However, the problem is solved when working in the IDE.
  • Compiling Scala takes longer than Kotlin.

Community and the future of the language

Scala exists since 2003, so it's no wonder that for such a time the language managed to gather around itself a large community of developers. Kotlin is younger, but JetBrains actively support their creation and contribute to its promotion. So, full support for Kotlin will be added to the new version of Android Studio, for example.

In terms of further development on Scala, its JVM compatibility is giving more and more influence: the speed of new functions is slowing down, unresolved problems can be inherited from the version to the version. Kotlin, having set himself the task of not stagnating as Java, has so far coped with it, significantly expanding the arsenal of software.

Conclusion

Kotlin  Scala
Mainly uses object-oriented paradigms with a little support of functional ones Symbiosis of OOP and FP
Simple language studying for Java developers. Scala, is an independent language, with all the resulting difficulties of study.
Convenient functionality for Null Safely. Not convenient functionality for Null Safely.
Faster compilation Slower compilation
Community is in the process of formation. Formed community.
The language is perfect for introducing into existing Java projects and rewriting them to a more convenient and flexible Kotlin. A multifunctional language that can be used both in Java-applications and other projects that are difficult to implement in pure Java.

 

What technology do you choose, Kotlin or Scala?

What programming language do you prefer? Kotlin or Scala? Or maybe you are already using some of them? Please, share your thoughts with the community. Also, after the voting, you will be able to see what people like the most. Your opinion is very valuable for the Hype.Codes team.

How to convert list to map in Kotlin

Two ways to convert list to map in Kotlin, with code examples
31 October 2017   505

For example, you have a list of strings like:

val list = listOf("a", "b", "c", "d")

and you need to convert it to a map, where the strings are the keys.

There are two ways to do this:

The first and most performant is to use associateBy function that takes two lambdas for generating the key and value, and inlines the creation of the map:

val map = friends.associateBy({it.facebookId}, {it.points})

The second, less performant, is to use the standard map function to create a list of Pair which can be used by toMap to generate the final map:

val map = friends.map { it.facebookId to it.points }.toMap()