Kotlin logo

Kotlin

A concise multiplatform language developed by JetBrains

Releases

Kotlin 1.0 Beta 3 is Out!

We are glad to present another update of Kotlin 1.0 Beta. We are working towards finalizing the standard library and getting rid of old deprecated constructs in the language, as well as bug fixes, performance improvements and future-proof checks.

Full list of changes is available here.
See closed issues here.

Library Changes

We are working hard on the standard library to get it into the best shape before 1.0. This involves some experimentation, so new deprecations happen and new functions are added. We are planning to make the final clean-up of the standard library in the 1.0 build (or RC): remove all the deprecations and other legacy stuff.

Here we give only one highlight from the changes: contains() and other similar extensions now accept supertypes of the element of the collection.

// strs: Collection<String>
// ns: String?
// cs: CharSequence
// i: Int
strs.contains(ns) // accepted now
strs.contains(cs) // accepted now
str.contains(i) // ERROR (in fact, a deprecation warning, but will be an error soon)

We found that the previously proposed containsRaw approach is inefficient, and opted for making contains() a bit more permissive, while keeping the initially intended safety. Note that the collection interfaces themselves are intact, and all this is done solely through extension functions. Use Code Cleanup to migrate your code.

Language Changes

Some highlights from the language changes, the full list is available here.
Many things that we deprecated before, have now become errors. Use Code Cleanup to migrate.

When expressions

This kind of code has proven to be problematic, so we decided to deprecate it:

when {
    foo.isValid(), foo.isReady() -> process(foo)
    ...
}

Many people tend to think that the condition “foo.isValid(), foo.isReady()” means that foo is both valid and ready, while actually the comma means or. The workaround is trivial: simply use || instead:

when {
    foo.isValid() || foo.isReady() -> process(foo)
    ...
}

Code Cleanup will migrate it for you.

Annotations

A bug has been fixed that prevented us from using arrays in default values for annotation parameters:

annotation class Entry(val value: String)

annotation class Example(
        val entries: Array<Entry> = arrayOf(Entry("a"), Entry("b")) // OK now
)

Enum.values()

Recently we changed the traditional Java’s Enum.values() to be a property: Enum.values, but now we are rolling this change back, because there’s an unpleasant corner case: a constant in an enum may be named values, and there’s no way to access one of the two then. We considered different options, and decided that changing values back to a function is the cleanest.

So, the values property is now deprecated, and values() function — un-deprecated.

Visibilities and scoping rules

We are cleaning up and fixing minor issues in visibilities and scoping rules, so

  • protected members are allowed in companion objects
  • Calls to non-@JvmStatic protected members of companion objects from subclasses are marked as errors (unsupported)
  • private setters are now deprecated for open properties
  • Local sealed classes are deprecated (never were usable)
  • Overriding setter cannot weaken visibility
  • Inner classes are no longer allowed inside enum entries
  • Use of uninitialized variables in lambdas / object literals / local functions is forbidden

Android Extensions

We have merged the main Kotlin plugin for IntelliJ IDEA and the Kotlin Extensions For Android plugin. The latter is now obsolete as its functionality is available from the main Kotlin plugin.

Also, we have added support for Android product flavors: now properties from different flavours are available in different packages.

For example, if we have two flavors in the build.gradle file:

productFlavors {
    free {
        versionName "1.0-free"
    }
    pro {
        versionName "1.0-pro"
    }
}

We can now use synthetic properties not only for layouts in the main source set, but also for the flavor layouts:

// Import synthetic properties for the `activity_free.xml` layout in the `free` flavor
import kotlinx.android.synthetic.free.activity_free.versionMarker

class FreeActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        ...

        setContentView(R.layout.activity_free)

        ...

        versionMarker.text = "Free version"
    }
}

Note that all layouts for the main source set are now located under the kotlinx.android.synthetic.main package, and the old package naming convention is deprecated.

What’s new in the IDE

  • Android Extensions plugin has been merged into the main Kotlin plugin and no longer needs to be installed separately
  • We’ve added option to choose Kotlin when creating a new Gradle project:
    Screen Shot 2015-12-02 at 19.54.03

  • Debugger: stacktrace navigation now supports stack frames from inline functions. Also there were a bunch of improvements in a stepping through inline functions.

  • Three new property initialization Quick Fixes have been added:

  • Introduce Variable (Ctrl+Alt+V / Cmd+Alt+V) now supports multi-declaration expressions:

  • Also it allows choosing container for expression in lambda or anonymous functions:

  • Beta 3 brings support of Introduce Variable/Parameter/Property/Function from string template fragments

  • Finally, one experimental feature has been added — basic support for Kotlin script files (.kts) in the IDE

image description