Kotlin M1 Candidate

IntelliJ IDEA 11.1 has been recently released, and we are happy to announce a milestone candidate build for Kotlin IDE plugin, too. This post gives an overview of what happened over the last month.

Milestone Candidate Build is Ready for Your Evaluation

To install on IntelliJ IDEA 11.1 (Free Community Edition is available here), please follow the instructions from the Getting Started guide. In short:

You can always download nightly builds of Kotlin from our build server or build it yourself from sources.

Now we proceed to a short overview of the New and Noteworthy. Please refer to this blog post for the previously implemented features.

Little Things that Matter

First of all, we did very many bugfixes, improvements and other important things that are hard to demo. See the commit history on github and the closed issues in YouTrack.

Library

With the power of extension functions, Kotlin makes existing Java APIs better. In particular, we provide enhancements for JDK collections so that you can say things like this:

fun main(args : Array<String>) {
    val list = arrayList(1, 2, 3)
    val odds = list.filter {it % 2 == 1}
    println(odds.join(", "))
}

Here, filter() and join() are extension functions.

Implementation-wise, extension functions are just static utility functions, like “good old” Java’s Collecions.*, but with the “receiver.function()” call syntax, the IDE makes them much better: there is code completion that helps you browse through the API and learn it (just as if the extensions were normal class members):

You can navigate to sources of library functions:

And see the doc comments there:

The HTML version of the library docs is available here.

GitHub Support

Kotlin highlighting is now supported by github, including gist.

Annotations

Kotlin now supports annotations. Here’s a small example that relies on JUnit 4:

import org.junit.Test as test
import org.junit.Assert.*

class Tests {
    test fun simple() {
        assertEquals(42, getTheAnswer())
    }
}

Read more here.

String Templates

Now you can use multi-line string templates, for example:

println("""
  First name: $first
  Last name: $last
  Age: $age
""")

Simple Enums

Simple cases of enum classes are now supported. For example:

enum class Color {
  RED
  GREEN
  BLUE
}

Local Functions

Functions can be declared inside other functions:

fun count() : Int {
  fun count(parent : Entity) : Int {
    return 1 + parent.children.sum { count(it) }
  }
  return count(this.root)
}

Nullability

Kotlin now recognizes the @Nullable and @NotNull annotations). If the Java code says:

@NotNull String foo() {...}

Kotlin will trat foo() as returning a non-nullable String.

A short-hand operator (!!) for converting a nullable value into a non-nullable one is added:

val foo = getSomethingThatMayBeNull()
foo!!.bar() // throw NPE if foo is null, run bar() otherwise

Byte Code Unveiled

Click on the Kotlin button on the right edge of the IDEA window, and choose the “Bytecode” tab. You’ll see the byte-code Kotlin generates for your program!

Your feedback is very welcome. Have a nice Kotlin!

About Andrey Breslav

Andrey is the lead language designer working on Project Kotlin at JetBrains. He also works on making the Java language better, serving as a Java Community Process expert in a group for JSR-335 ("Project Lambda"), and occasionally speak at software conferences such as Devoxx or JavaOne.
This entry was posted in General and tagged . Bookmark the permalink.

15 Responses to Kotlin M1 Candidate

  1. Loic says:

    Thanks for this great work!

  2. jenniferashleykuiper@gmail.com says:

    Failed to load descriptor. :( (Mac Lion)

  3. jenniferashleykuiper@gmail.com says:

    Resolved now :D

  4. If foo!!.bar() raises NPE if foo is null, how exactly does that differ from simply calling foo.bar()?

    • Nikolay Krasko says:

      After !!. operator was added to Kotlin program there’s no difference in behavior from foo.bar() call in Java. But the great difference was at previous step when compiler was able to find a place with probable NPE threat and asked programmer to resolve it explicitly.

      You can find more information about null-safety feature in Kotlin on this wiki page.

      • Justin Lee says:

        So if I read that correctly, without !!, the compiler will fail on a possible null reference. But the !! tells the compiler to butt out and let it fail at runtime?

        • Nikolay Krasko says:

          Yes you’re right. And this is the reason why !! calls should be used with great caution or avoided completely. If you are in Kotlin and not planning to assign null value to variable just declare it with not-nullable type. While working with methods and variables from Java it’s better to choose some safe way to deal with possible null value. It can be ?. call or pre-check to null.

    • Hey Charles,

      Glad to see you here!

  5. Alex Yakunin says:

    Guys, what you do is awesome!

    Personally I’d switch to Kotlin right after first RC.

  6. Sergii Rudchenko says:

    Thank you guys, this is a brilliant work!
    I’ll switch to Kotlin as soon as it gets Android support.

    Also Kotlin has inspired me to switch to IntelliJ IDEA :)
    Keep up the good work!

  7. Guangyu says:

    How to use JUnit with kotlin? or how to have unit test with kotlin? Can you provide any example on it ?

    I found compile speed will become very very slow, and KCompile will report the following error:

    java.lang.IllegalStateException: Internal error: (14,72) java.lang.AssertionError
    @ClosureAnnotator.java:112
    at org.jetbrains.jet.codegen.CompilationErrorHandler$1.reportException(CompilationErrorHandler.java:27)
    at org.jetbrains.jet.codegen.GenerationState.compileCorrectFiles(GenerationState.java:120)
    at org.jetbrains.jet.compiler.CompileSession.generate(CompileSession.java:161)
    at org.jetbrains.jet.compiler.CompileEnvironment.compileModule(CompileEnvironment.java:156)
    ... 89 more

    even for the following code:


    package hhh.csp

    import org.apache.log4j.Logger
    import org.apache.log4j.PropertyConfigurator
    import org.junit.*
    import org.junit.rules.ExpectedException
    import junit.framework.TestCase
    import junit.framework.TestSuite

    public class IntervalTest(name :String) : TestCase(name) {

    fun testIntervalPlus() {
    val values : Array = Array(10000,{Interval.zero()});
    var v:Interval = Interval.zero();
    for( i in values ) i.u = 1.0;
    for ( i in values) v = v+i;
    println(v);
    }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>