Kotlin vs Scala

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

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.

JetBrains to Roll Out Kotlin/Native 0.8

Version 0.8 of Kotlin/Native brings new features and bug fixes
13 July 2018   137

The project team of Kotlin / Native introduced the release of version 0.8. Corrections and additions have concerned the security of multithreaded programming, the expansion of library functionality and support for iOS devices. As always, alot of bugs are fixed.

Parallelism

Implemented the concept of object freezing

object Config {
  var width: Int = 10
  private set
  init {
    val file = fopen("config.txt", "r")
    if (file != null) {
              try {
              val buffer = ByteArray(2 * 1024)
                 while (true) {
                 val nextLine = fgets(buffer.refTo(0), buffer.size, file)?.toKString()
                 if (nextLine == null || nextLine.isEmpty()) break
                 val records = nextLine.split('=')
                 if (records.size != 2) continue
                 val key = records[0].trim()
                 val value = records[1].trim()
                 when (key) {
                         "width" -> width = value.toInt()
                            }
                          }
                        }
             } finally {
                fclose(file)
                    }
         }
}

This code considers the configuration file and will later receive the Config.width property. 

The Config file itself is divided for any number of threads or vorkers. After publication, the object is frozen and any attempt to change it will result in an exception (InvalidMutabilityException).

The second peculiarity of parallelism in the Kotlin / Native project is indivisible values and references. Let's add an image:

object Config {
private val icon_ = AtomicReference()
val icon: Image
get() = icon_.get()!!
fun updateIcon(file: String) {
val imageData = Image(file)
icon_.compareAndSwap(icon_.get(), imageData.freeze())
}
}

All information about the icon in the AtomicReference is frozen. Any changes are indivisible and similar information is visible to each client.

Kotlin Libraries

Starting v 0.8, the standard library (kotlin. *) is compatible with other platforms. Standard number generator and the function of mixing the collections are designed to eliminate the use of sharpened for a specific platform API. As an experiment, the project supports other libraries, for example, kotlinx.coroutines, kotlinx.serialization, and also the Ktor HTTP client.

Changes for iOS devices

Fixed issues with the publication in the App Store and the initialization of the platform. Support for the 32-bit ARM architecture of iOS processors, which allows using Kotlin / Native on older devices from Apple added.