Skip to end of metadata
Go to start of metadata

What is Kotlin?

"Project Kotlin" is the codename of a statically-typed JVM-targeted programming language developed by JetBrains intended for industrial use.

Why a new language?

At JetBrains, we’ve been developing for the Java platform for a long time, and we know how good it is. On the other hand, we know that the Java programming language has certain limitations and problems that are either impossible or very hard to fix due to backward-compatibility issues. We know that Java is going to stand long, but we believe that the community can benefit from a new statically typed JVM-targeted language free of the legacy trouble and having the features so desperately wanted by the developers.


The main design goals behind this project are

See Comparison to Java and Comparison to Scala.

Is this an open source project? What's the license?

Both the compiler and the IntelliJ IDEA plugin are open source under the Apache 2 license. We're happy to accept contributions.

Is it Java-compatible?

Yes. The compiler emits Java byte-code. Kotlin can call Java, and Java can call Kotlin. See Java interoperability.

Other platforms than Java?

We have started a JavaScript back-end recently. There are external Kontributors willing to work on an LLVM back-end.

Eclipse-based IDE?

We plan to provide two things:
1) An open source Eclipse plugin that will be initially contributed by JetBrains, and later we will gradually move it's support onto the community.
2) API exposed by the compiler, to make it easy to retrieve semantic information from within the plugin.

In any case, Eclipse support will be released later that IntelliJ support.

Is this a functional language? Is this an object-oriented language?

This is an object-oriented language. It supports higher-order functions and function literals, but that does not make it a functional language. It targets OO-developers.

What about generics?

Kotlin has generics. They are retained at runtime, support declaration-site variance and usage-site variance, and Kotlin does not have any wildcard types.

Does it have type inference? Is it a good idea?

Kotlin infers type arguments when generic functions are called, as well as types of variables from their initializers. This makes the code more concise.

Does it have semicolons, curly braces, etc?

Semicolons are optional. Curly braces are there. Types of variables and functions are written on the right after a colon (like Scala, and unlike C-style languages).

Why types on the right?

We believe that it makes the code more readable. Besides, it enables some nice syntactic features, for example, it's easy to leave type annotations out, and Scala has proven pretty well that this is not really a problem.

Is this language extensible?

We are planning to make it extensible in a few ways: from inline functions to annotations, type loaders and language-quotations.

Can I embed my DSL into this language?

Yes, Kotlin provides a few features that help here: Operator overloading, Custom control structures via inline functions, Infix function calls, Extension functions, annotations, language-quotations.

Does it have IDE support?

Yes. The compiler is developed as an IntelliJ IDEA plugin, and user-facing IDE features are there from the very beginning (we make good use of them while debugging and testing).

Where to ask more questions?

Join our Forum.

What to read next

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Jul 20, 2011

    Some more questions: 

    Where can I get the compiler? 

    How are dynamic builders implemented? 

    1. Jul 20, 2011

      We are planning a public Beta for the end of 2011.

      Kotlin will have the dynamic type (very much like C#), and it will be the basis for dynamic builders.

  2. Jul 20, 2011

    Anonymous

    Why, oh why types on the right?No IDE can suggest variable names this way!

    Please rethink about that.

    1. Jul 29, 2011

      Anonymous

      Yes it can. It can suggest a variable name after the "val" or "var" keyword is typed.

      It can also suggest a type name after the ":" is typed.

    2. Dec 21, 2011

      An IntelliJ Live Template that inserts the type before the variable name would solve that problem.  The type would be known when the variable name was inserted so IntelliJ could suggest a variable name based on the type.

  3. Jul 20, 2011

    Anonymous

    Will the code compiled by Kotlin have dependencies?

    Is there a .jar file that one needs to have on the CP in order to run Kotlin-compiled programs?

    1. Jul 21, 2011

      Yes. Some features (reified generics in particular) require us to have a small runtime library that will be a dependency for all Kotlin programs.

  4. Jul 21, 2011

    Anonymous

    What are the plans for "Kotlin collections"?, in my opinion the collection framework is the definitive driver to change the language used in "everyday tasks".

    Regards,

    Germán

    1. Jul 22, 2011

      There will be a collection library in Kotlin standard library. The exact design is not fixed yet.

      1. Jul 26, 2011

        Glad to hear.  My main issues with the JDK collections (ignoring those forced by the language, e.g., lack of extension methods which Kotlin addresses well) are:

        1. Mixing mutable & immutable operations.  I'd prefer not to need wrappers such as Collections.unmodifiable* when a simple cast to the immutable base interface does the job, etc.  Likewise Iterator.remove().
        2. No specialization for Iterator that includes size().  This comes up when optimizing collections and algorithms that work with iterators.
        1. Jul 29, 2011

          Anonymous

          Personally, I would like a set of immutable collections in addition to the mutable ones, so that I don't need to copy them when passing them off to functions that are not supposed to modify them, possibly for security reasons. Both the mutable and immutable versions could implement the same set fo immutable interfaces, though. (The mutable versions would implement mutable interfaces as well.)

          I really miss simple things like having a method that returns the union of two sets without modifying either of them. Java makes this unnecessarily difficult.

  5. Jul 21, 2011

    Anonymous

    1. Jul 27, 2011

      Mirah is basically another syntax for Java (currently, Java 1.1.x, but this will improve, I think). And this way you get Java, and not another language.

  6. Jul 21, 2011

    Anonymous

    My wish would be for Katlin to only have immutable variables and, if at all, only managed mutable variables.  Thoughts?

    1. Jul 22, 2011

      What do you mean by "managed mutable variables"?

  7. Jul 21, 2011

    Anonymous

    What about an Eclipse plugin?

    1. Jul 22, 2011

      We are considering having one

      1. Jul 30, 2011

        Anonymous

        I think an Eclipse plugin will be necessary if you want this language to get any kind of widespread adoption. A LOT of people aren't going to want to switch to Idea just to use a new language. Especially not before they're fully committed to that new language.

        1. Feb 21, 2012

          So you're saying you can switch the language (and, maybe, the entire philosophy of how you develop), but can't afford switching IDE?

  8. Jul 22, 2011

    NB - I just read on the Classes in inheritance page than "final" is an annotation.  I've removed the body of the comment accordingly.

  9. Jul 22, 2011

    Anonymous

    I think it would be a strong advantage if you could include typestate checking in the language and IDE.

    see Aldrich et al, 2009, Xu Yu 2006 and DeLine et al. 2004

    1. Jul 29, 2011

      Anonymous

      I second the desire for typestate checking. See also the "Rust" programming language's use of it.

      To do typestate checking properly, it may need to be necessary to have a way to declare a function as "pure" (having no side effects) so that the compiler can feel free to optimize away unnecessary calls to it.

      1. Jul 29, 2011

        Well, all this doesn't play to well with concurrency and encapsulation.

        1. Jul 30, 2011

          Anonymous

          I don't think you've understood it fully. Concurrency is one of the primary drivers of it, actually. If you have functions that are known to be pure, then you don't care what order they're called in, because there is no interaction between them. You also know that if the same function call is made with the same arguments, that the same function return value will result from it. Those two items together can tremendously enhance the compiler's ability to optimize code without breaking things. Additionally, it enables the compile-time assertion checking and limited program proving of the typestate mechanism. (Essentially, typestate can perform as a limited form of compile-time precondition/postcondition checking).

          As far as encapsulation, that is a programmer's concern, not a compiler's. A compiler's job is to make things run really fast. Breaking down encapsulation (when doing so provably won't break the code) is a great way to increase the performance of code.

          Anyway, the idea of typestate is that pure boolean function calls can be used as compile-time assertions about relationships between variables and expressions. if I declare a pure boolean function, such as:

               pure boolean hasPositiveSin(double x) {return Math.sin(x) > 0; }

          and I have a statement at the beginning of a different function like:

               if (!hasPositiveSin(x)) {throw new SomeKindOfException()}

          or, more concisely,

               check_state hasPositiveSin(x)

          at one point in the code, then since the "hasPositiveSin" function is known to be pure, a later call in the same function which checks the same condition must have the same value. Thus if I have later code such as:

               if (hasPositiveSin(x) || someOtherCondition(x,y)) { doSomething(); }

          the compiler knows it can simplify this to:

               if (true || someOtherCondition(x,y)) { doSomething(); }

          which can be further simplified to:

               doSomething();

          and likewise if it encounters a later check_state statement that asserts something contradictory to a previous one:

              check_state !hasPositiveSin(x)

          it can alert the user to a compile-time error, since the compiler can prove that if flow control went from the first check_state statement to this one, that this one will always fail.

          note that the compiler never had to actually EVALUATE the hasPositiveSin(x) function at compile time. And also note that the only actual call that it inserted to it in the bytecode was in the first check_state call, because in that case it couldn't prove its value to be true or false. All of the other calls to hasPositiveSin(x) were optimized away.

          Thus the user can define a set of pure boolean functions that assert a variety of useful properties about whatever items they care about (e.g. "pure boolean containsOnlyEvenNumbers(List<T> items)"). The compiler doesn't need to actually evaluate those functions at compile time -- it just needs to use the CALLS to those functions to determine if the various check_state statements are consistent with each other. In cases where it can't determine that, it inserts actual calls to the functions in a similar fashion to Java's 'assert' mechanism.

          1. Jul 30, 2011

            If you call a pure function on something twice, it's guaranteed to return the same value only if that something you call it on has not been changed by another thread in between the calls. And this is rather hard to guarantee statically.

            1. Aug 04, 2011

              Anonymous

              The compiler doesn't need to prove the function is statically pure. (It might, however, be nice for the IDE to warn if such a function does things like reading from mutable fields). If a user has marked a function as pure, then by doing so the user has asserted that the function can and should be treated as such by the compiler. If the user lied, that's the user's fault. The compiler should feel free to perform optimizations on any funtion declared with the "pure" annotation, regardless of what the function body actually does. (Note that even pure functions can sometimes modify state for purposes such as caching.)

              1. Aug 04, 2011

                The whole point of statically-typed languages nowadays is to prevent those "user's faults". What you propose is a gigantic puzzler-generator.

                1. Aug 14, 2011

                  Anonymous

                  I'm not suggesting that preventing user's faults isn't desirable. I'm just noting that it isn't strictly necessary in this case. It would still be highly desirable if the compiler and/or IDE could do so, however. Particularly if they complained about a function that was declared "pure" but appeared to be doing possibly-impure things. Possibly the user would have to add the equivalent of "@SuppressWarnings("functionDeclaredPureDoesNotLookPure")" to suppress these warnings if the IDE and/or compiler thought something looked too suspicious.

                  Note that the 'assert' statement in Java takes as its parameter an expression that is supposed to be pure, and which could call pure functions. Calling a non-pure function from within an 'assert' statement will likely cause code errors when assertion checking is enabled or disabled. However, Java has no checking for this whatsoever, so the user is free to call any functions that he wishes from an 'assert' statement. This is unfortunate, but at the same time it hasn't stopped people from being able to use the 'assert' statement. They just maintain the knowledge of what functions are supposed to be pure or not in their heads, and with comments in their code, and with reasonable function naming ("isEmpty(List)") and so forth, and resolve never to call any methods but those from an 'assert' statement.

                  Having a 'pure' keyword at least DOCUMENTS what functions are supposed to be pure, and gives some hope that the compiler and/or IDE, and/or human code reviewer might have a chance at figuring out when this is not the case. It also warns users that if they call a function which is not marked 'pure' from a context that demands purity (such as a check_state statement or an assert statement) that they will get a compiler error. If such functions are not documented in a machine-readable way at all, there is no way to detect violations like this.

                  1. Aug 14, 2011

                    This can be done with a custom annotation + an optional annotation processor.

                    1. Oct 25, 2011

                      Anonymous

                      The key problem there is the word "custom". When you allow users to define their own, everybody does it differently, and no two users do it the same. This makes interoperability between libraries written by different people mostly impossible.

                      Consider, for instance, the chaos that existed in C++ with the wide variety of different 'custom' types created to hold booleans before the language designers finally added a standard 'bool' type to the language in an attempt to control the chaos.

                      Likewise, the wide variety of different custom and incompatible mechanisms in Java for doing runtime assertion checking before the 'assert' statement was added.

                      1. Oct 25, 2011

                        You are right. Unfortunately, we can't standardize all the things in the world. So, our current strategy is to avoid adding unsound checks to the compiler, leaving them as possible custom extensions.

      2. Feb 23, 2012

        Please take my vote against typestates. We have already seen languages which try to embrace every programming paradigm in the world (and yes, I mean Scala).

        My personal opinion is that the core of the language should be small, concise and open to extensibility via libraries, modules and plugins (which, of course, should not be included into the language microkernel).

  10. Jul 23, 2011

    I may have missed it elsewhere in the wiki.  What will your equivalent of javadoc look like?

    1. Jul 26, 2011

      Same /** */ comment style, but wiki markup instead of HTML

  11. Jul 24, 2011

    Anonymous

    Will the syntax support regions like C# for code folding?

    Regions in C#

    Best regards,
    JMP

    1. Jul 26, 2011

      We are not planning to support this

    2. Jul 28, 2011

      Note that folding regions are purely an IDE feature; there's no need to have special syntax in the language to handle them.

      1. Jul 28, 2011

        Are you familiar with C# regions? They are used to denote fold groupings *other* than those automatically provided by IDEs. They are most often used to delineate the boundaries of machine-generate code, and some programmers like to use them to group up sibling methods, fields or classes into logical groups.

        Personally, I don't much like them. I thought they were great when I first learned about them, but after using them for a while I changed my mind. For machine-generated code, C# now has partial classes, which in most cases are a better way to separate that code. For programmer-selected groupings, I find that they are an annoying distraction more often than a helpful tool. If there is so much uninteresting boilerplate that it's important to fold it away with a region, it's probably indicative of either a deficiency in the language or in the design. Plus if there's enough stuff in the region that it's worth hiding, there's a good possibility that there are bugs hiding in there too.

        1. Jul 28, 2011

          I certainly am familiar with the C# regions. This is a feature that exists purely for IDE use, and if we wanted to provide support for this in the IDE, we would be able to do so without introducing special syntax in the language. For example, NetBeans does that for Java.

          1. Jul 28, 2011

            Oh, right, with the special comments? Sorry, when you said no "special syntax", for some reason I thought you meant no markers at all. I see what you mean now.

            1. Jul 30, 2011

              Anonymous

              Does IntelliJ IDEA support custom folding areas like NetBeans? Or is there a plug-in with this feature?

              The folding in NetBeans looks like this:

              The XML-like notation doesn't look very nice, but the feature is good.

              Partial classes are good to separate big parts of code, but with regions you can group smaller parts, e.g. the overloads of a method.

              Will Kotlin support partial classes?


              JMP

              1. Aug 01, 2011

                We believe extension functions pretty much cover partly-generated code scenario for partial classes, so no, Kotlin will not have those.

                1. Aug 02, 2011

                  Does IntelliJ IDEA support custom folding or is it planned? Or does a plug-in exist that enables this feature?

                  1. Aug 02, 2011

                    Yes, it does support custom folding, but I mean we can generate a bunch of extension functions to a separate file, that's why we don't need С# partial classes.

  12. Jul 24, 2011

    Anonymous

    What are language quotations?

    1. Jul 26, 2011

      This is not a detailed design yet, but the idea is that you can say, for example

      [sql] "SELECT foo FROM bar"

      And the string will be parsed statically as SQL, with all the according checks

      1. Oct 25, 2011

        Anonymous

        For conciseness, I'd prefer something like:sql:"SELECT foo FROM bar"

        You will of course also need to consider multiline strings.

        1. Oct 25, 2011

          We can's say sql:"...", because "a:b" means "type of a is b", but I get your point about a more concise syntax.

          We will definitely have multiline strings.

          1. May 13, 2012

            What about

            it's look like functions. Is it possible to get result from function?

  13. Jul 24, 2011

    Has there been any thought into how reflection and mocking would be handled in Kotlin?  The lack of interfaces and closed-by-default-classes leave me wondering how you'd go about writing tests for Kotlin code...

    1. Jul 26, 2011

      We are considering a special build mode for tests that makes everything public, open and virtual. There are pros and cons there.

      1. Jul 27, 2011

        Hrm - not 100% sure I'd like that - as you say pros and cons.  I wonder, if Kotlin was to target JDK7 and above ( maybe a fallback for earlier JDKs ) add support for a dynamic type along the lines of:

        If that handle reflection/InvokeDynamic under the covers for you ( or, given we're statically typed - if the compiler can resolve the method, call it directly as normal ) then I suspect we might be in a fairly sweet place.  Combine that with some mocking extension methods I think a fairly nice mocking framework could be done.

  14. Jul 30, 2011

    Are you folks planning on using Java's thread model? Without requiring VM changes, would it be possible to do more of an actor-based model?

    1. Aug 01, 2011

      Well, Scala is implementing its actors over same VM, so it is definitely possible.

  15. Jan 09, 2012

    Will Kotlin compiler generate Java source code? I see at least two reasons for it:

    - Google Web Toolkit work with Java source code and not with JVM bytecode. Compilation to Java source allows to program in Kotlin with GWT.

    - Minimize risks of using of new "green" language in real project development. When code written in Kotlin can easily be converted to Java a project manager may permit experimental using of Kotlin while there will be a way to return to old Java.

    1. Jan 09, 2012

      There are a few things to note here:

      1. Generating Java source code introduces unneeded complications, and some things (like having no checked exceptions or making some methods synthetic) are simply not available.
      2. We reuse the GWT back-end anyway.
      3. The generated Java code wouldn't be pretty enough to be checked into a production repository anyway.
  16. Jan 18, 2012

    Clojure has the notion of a standard library (called core) and a collection of generally useful blessed libraries (called contrib). The libraries in clojure-contrib are not maintained by the Clojure team, but are considered of high enough quality to deserve special mention (think Apache Commons and the like), and are kept in Clojure's infrastructure.

    Will Kotlin have a Kotlin-contrib or something of the sort? It seems like it would be an interesting way to engage the community, and a good place for some of the suggestions made here (like in the typestate discussion above).

    This Google Groups thread is particularly interesting for discussing contrib's model and raison d'etre.

    1. Jan 18, 2012

      I think that the overall idea of having contrib make a lot of sense. Currently it is maybe a little early for that, but it may be a matter of months... Thanks for the suggestion!

  17. Feb 20, 2012

    Hi guys,

    What syntax is planned for defining an annotation (with runtime retention)?

    1. Feb 22, 2012

      // Declaration 
      annotation class brilliant 
       
      // Usage 
      brilliant class Foo
      1. Feb 23, 2012

        Now that's totally awesome! No, really. You actually provide DSLish tools for metainformation. This could be a whole new experience for frameworks which heavily rely on annotations (such as ORMs).

        I also assume (please correct me if I am wrong) it is possible to annotate everything in such a way, like:

        1. Feb 23, 2012

          Yes, you can annotate any declaration is such a way