What's New In Kotlin Version 1.2?

Publicado el - Última modificación el

The beta version of Kotlin 1.2 is out and offers an experimental feature that allows the reuse of code across supported target platforms. The new version is also compatible with the new Java 9 module system. The new release focuses more on bug fixes in the compiler, tools, and improvements in the standard library.

A common module holds the code the developer will share on the target platforms – parts of the code that are platform-dependent are kept in platform-specific modules. When it comes to compilation, code is generated for both the platform-specific and common parts.

The standard library of Kotlin 1.2 still has the kotlin.math package for performing mathematical processes in cross-platform code. Kotlin’s Version 1.2 standard library is also compatible with the new module system of Java 9 – which does not allow multiple .jar classes in the same package.

The artifacts kotlin-stdlib-jdk8 and kotlin-stdlib-jdk7 takes the place of kotlin-stdlib-jre8 and kotlin-stdlib-jre7 found in the older version. To make Kotlin 1.2 compatible with Java 9 it was necessary to get rid of the kotlin.reflect package earlier present in the kotlin-reflect library.

Type inference improvements in the version 1.2 include the ability to let the compiler adopt information in type inference extracted from type cast. It also has the following enhancements;

  • It has more consistent syntax.

  • It simplifies coding because it supports an array of literals in annotations.

  • The lateinit modifier can now be used on local variables and top-level properties.

  • Developers can now check whether the initialization of the lateinit using the reflection API.

The Kotlin language was modeled for JVM but has expanded to compile to Javascript. Kotlin got a boost when Google endorsed it as a mechanism for building Android apps for mobile – alongside Java. Luckily for Kotlin developers, all the scheduled features for the Kotlin 1.2 featured. Some of them are:

Errors And Warnings

In the Kotlin 1.2, developers have the option of treating warnings as errors. There are two ways you can achieve this. Either you go to the command line and type –Werror, or you add the line

compileKotlin {

kotlin0ptions.warningsAsErrors = true

}

Multiplatform Projects

The multiplatform project of Kotlin, which is still under experiment, was initially compatible with JavaScript and JVM, and in later versions, Native. If you have tried this project, you will need to change header and impl with actual and expected.

Developers can now express the reliance of mutual code on fragments that are platform-specific through actual and expected declarations. A definite statement can either be a type of code-name that points to a standing operations of the API present in an exterior library, or platform-specific to the API.

Grouping Literals In Annotations

Kotlin 1.2 features a novel semantic which supports grouping literals in annotations. This helps to shorten the codes developers write. For example, the literal expression @CacheConfig(cacheNames = ["books", "default"]) was formally @CacheConfig(cacheNames = arrayOf("books", "default")).

This feature was available in the previous version but the syntax was made more consistent in the Kotlin beta 1.2 version. It also permits the usage of array literals on vararg factors:

@RequestMapping(value = ["value1", "value2"], path = ["path1", "path2"])

To effect this change, adjustments were made on the syntax using varargs together with named arguments in annotation and consistent technique calls. There is a plot to enforce a compiler optimization to omit the array portion in certain calls.

Lateinit Improvements

A fresh image API was incorporated into the Kotlin 1.2 to allow the developer to verify if lateinit adjustable has been primed. This is as follows:

lateinit var file: File

// ...

if (::file.isInitialized) {

 ...

}

Bear it in mind this is not a complete implementation, but only a part of the KEEP proposal. The rest part (deinitialize method) will likely feature in the next version (Kotlin 1.3).

The lateinit transformer is now available for use on local variables and properties at top-level. The former is applicable when resetting a graph of object with a round reliance stuck between the properties of the graph’s object. A good example is when the lambda passes as a creator dispute to an object which makes reference to another object with a later definition:

fun test() {

 lateinit var x: Component

 // inject takes a lambda which must return the Component though

 val injector = inject(

   ...,

   componentProvider = { x },

   ...

 )

 // Initialization is only possible via injector, once it has been run

 x = injector.createComponent()

}

Guaranteed Callable Location Improvements

When you have an expression that reads this::foo, it is now possible to do away with the “this”. The command creates a callable reference tied to an affiliate of “this”. You can write the code now as: foo. Before now, a vacant left sided syntax was only applied to generate a callable reference to a declarations at top-level.

Improvements On Type Inference

The previous version of Kotlin (version 1.1) was unable to infer a type argument for T in calls like <T extends View> T findViewById(int id). Data in type inference from type casts is now available for use by the Kotlin compiler.

In a generic call method where a type T parameter returns and the value is projected to a precise type Foo. The enhanced 1.2 is now equipped with a compiler that understands the T for this operation has to be tied to the type Foo to be executed. Android developers will find this tip invaluable because, unlike in the past, the compiler now accurately processes findViewById operation in the level 26 of Android API:

val button = findViewById(R.id.button) as Button

kotlin.math

Another new package in the Kotlin 1.2 standard library is the kotlin.math. The purpose of this package is to equip you with the power to handle mathematical operations in cross-platform code. Several improvements were made in the Beta 1.2 version including:

  • JavaScript now has improved precision of math polyfills.

  • It now supports inverse hyperbolic functions like asinh, acosh, atanh.

  • JavaScript now has binary representations functions of floating point numbers like toBits, nextUp and others.

Just When You Think You Have Seen It All

The Java 1.2 M2 is out and the warning is now replaced with “error”. This means that any of such codes will have to be compiled with -jvm-target 1.8 option. Hitherto, Kotlin interface members overrule Java-default methods while aiming at JVM 1.6 yields a caution on super calls:

Super calls to Java default methods are deprecated in JVM target 1.6. Recompile with '-jvm-target 1.8'.

Revised Pairwise And Windows Operations

The name of the pairwise function was too ambiguous. It did not provide hints on how the elements were paired together. Its new name is now zipWithNext to show that each element is a collection and zipped with the next.

The step parameters of the windowed function have 1 as its default. There is also an additional parameter, partialWindows, which is optional. Its purpose is to control what happens to an incomplete window at the end. Its default is false which means that incomplete windows will be dropped.

Math Operations

Developers have been clamoring for math operations support in standard library KT-4900. They will finally get their request in Kotlin 1.2 M2. Before now, you had to rely on the mercy of constants and math functions dug from java.lang.Math class in the JVM platform to kotlin.js.Math. This made visible Native JS Math utilities to Kotlin code when you were in the JS platform.

The following group of APIs will come with the kotlin.math package:

• Constants E and PI

• Cos, tan, sin and their inverse, acos, atan, asin, atan2 for trigonometric

• sinh, cosh,  tanh for hyperbolic

• log, log2, log10, ln, ln1p for logarithms

The upgrade of JavaScript is often quickly followed by the upgrade of languages that depend on it, as we can see with Kotlin. Milestone releases are not final and some of the features it contains are subject to change.

It is hard to find a developer that is unemployed but if you want to hone your skills on the use of the new Kotlin 1.2, you can log on to freelancer.com where you will find tons of programing jobs – some of which are peculiar to Kotlin 1.2.

Kotlin 1.2 certainly has promising features but do you think it has enough to oust its rivals and win the heart of developers? Leave your opinion in the comment box. Feel free to share this article with your friends.

 

Publicado 14 noviembre, 2017

TomCoulter

Designer // Writer // Creative

Tom is a Design Correspondent for Freelancer.com. He is currently based in Melbourne and spends most of his non-work moments trying to find the best coffee.

Siguiente artículo

Alexa: A 101