
Kotlin 1.1 Released with JavaScript Support, Coroutines and more - lynfogeek
https://blog.jetbrains.com/kotlin/2017/03/kotlin-1-1/
======
WkndTriathlete
A few weeks ago I wrote the same application four times in four different JVM
languages: Java, Groovy, Scala, and Kotlin, with a particular focus on the
application using functional techniques and types as well as being reactive. I
also included as goals complete test coverage with a preferred test framework
from each language and complete build tooling, all within a single Gradle
project.

The only significant sources of variance were the following:

a) Groovy's support for functional programming is starting to show its age. It
worked with everything, though.

b) The Scala 2.12 compiler has at least one bug that appears to affect its
interaction with Java 8 lambdas (in particular, in at least one case
expr.method() and (expr).method() will not pass the compiler's typecheck, but
{ val x = expr; x.method() } does. (All three pass IntelliJ's typechecker for
Scala.) Outside of this (aggravating) issue and slower build times, it did
work.

c) Java works well with everything but ends up being quite verbose, especially
in regards to a functional programming style (no surprise there).

d) Kotlin (1.0) works well with everything and is relatively succinct except
for Mockito due to everything defaulting to 'final' \- but it appears that
Kotlin 1.1 fixes this with a compiler plugin.

Based on my experience with all four languages, I would definitely try adding
Kotlin to projects. It seems to be surprisingly mature and stable given how
much less time it has spent in development (relative to the other three
languages.)

~~~
Scarbutt
Curious, if you were aiming at writing in a functional programming style, why
didn't you try Clojure?

~~~
tormeh
It doesn't have compile-time type checking. When many people say functional
code they really mean code that can be and is strictly type checked at compile
time. Clojure is functional, but it's not what most people want when they say
"functional".

~~~
mlmlmasd
Type-checking doesn't really have any bearing on weather a language is
functional. Functional languages can be statically or dynamically typed.

~~~
Sammi
It's one of the big differences between the lisp and ml types of functional
languages I would say. Other than the parentheses that is of course :)

I've never been comfortable doing big projects - multiple files, more than a
few hundred lines of code - in a dynamically typed language. It's the same
reason were seeing the big uptake of typescript. Statically typed languages
give us so many superpowers when we need to do refactoring or any changes at
all to an existing codebase. Just changing a function name in a dynamically
typed language can be hell. Rearranging the parameter order is even worse,
cause you have such trouble finding all of the function uses and where you
need to update. With statically typed languages you get editor support for
simple stuff like this and it becomes automatic and a push of a button almost.

~~~
mlmlmasd
Static typing is great. My point is that whether a language is statically or
dynamically typed has no bearing on whether it is functional.

------
vbezhenar
Kotlin for me is Java made right. It's not complex, it has awesome
interoperability with Java, it uses JDK, not reinventing its own wheels and it
has everything I need from modern language.

But it seems that Jetbrains has its own vision for Kotlin being a completely
independent language. So while it works for me right now, I'm not sure if it
will work in the future. They are going native and it'll bring a lot of
changes, for sure. Honestly I would prefer it to stay as Java enhancer, as I'm
not interested in Native and I need very little JavaScript interoperability,
if any (probably just translating some common code between backend and
frontend).

But we will see, I don't want to sound too pessimistic. Kotlin is the best
language I've used so far.

~~~
afastow
I completely agree.

Kotlin is close to the ideal language for me because it is just Java done
right. If they want to go native and can do it without impairing any of the
JVM usability, I guess go for it. But I don't see the point behind it. I don't
develop native apps so maybe I just don't know enough, but it seems like Go
and Rust have already satisfied the desire for modern natively compiled
languages.

I feel the same way about the Kotlin compiling to Javascript ability. I write
both the frontend and backend of web apps so I theoretically should be the
exact target market for Kotlin to Javascript but I have no interest in it. ES6
and/or Typescript work better there and have an entire ecosystem around them.
And even if they didn't work for me I'd prefer Dart which targets the frontend
first instead of being an extra like Kotlin Javascript.

My advice to Kotlin: Stick with what you do best. Kotlin is on the exact right
track on the JVM but I think all these different modes are going to make
people more cautious about adopting it in any of them because they'll worry
it's a jack of all trades but master of none.

~~~
meddlepal
Personally I'm intrigued by Kotlin Native as a replacement for Python and Go
tools without requiring the JVM.

I find it's a fundamentally better language than both and so being able to
write CLI tools and other smaller stuff in it would be nice while still being
able to leverage Kotlin + JVM on the backend would also be nice.

~~~
jug
Same - a common language for JVM, JS, Native is both unique and intriguing to
me. These are three really big targets. It's not that it would be unique in
being a higher level compiled language or a language making Java development
more fun to me, but being a common languge for among the most popular targets
besides .NET today.

~~~
tutanchamun
Scala already does that with scala.js (production-ready) and scala-native
(don't know if production-ready, probably not since it's only at 0.1 and they
want to implement a better GC instead of relying on boehm like now).

Just wanted to point that out in case you didn't know that already.

------
sandGorgon
Kotlin is not just for android apps.

Kotlin is getting first class support on next generation Spring web framework
with functional programming support -
[https://speakerdeck.com/sdeleuze/functional-web-
applications...](https://speakerdeck.com/sdeleuze/functional-web-applications-
with-kotlin-and-spring-5)

it's almost done with first class support for vert.x which is a high-
performance reactive webframework [https://github.com/vert-x3/vertx-kotlin-
common](https://github.com/vert-x3/vertx-kotlin-common)

The reactor project (erlang for the jvm?) is building first class support for
kotlin [https://github.com/reactor](https://github.com/reactor)

I think its going to be a great year for building backend systems on jvm.

~~~
asitdhal
Kotlin is the best option for Android apps.

There are many languages for backends and tons of libraries. But there is
hardly any substitute of Java in Android(if you don't consider JS solving
every problem in this planet). If JetBrains only concentrate on building an
ecosystem for android, it can become "the real Swift for android".

I built a dummy app for android in Kotlin. It was great. The same thing I
tried in Scala(it didn't go very well).

------
grabcocque
What I like about Kotlin is it's what you might call a "pragmatic Scala", and
could find a sweet spot between Java and Scala where its added expressiveness
allows it to shine, but Scala's complexity and performance issues aren't in
evidence to scare the development managers into staying with Java.

~~~
Scarbutt
_Scala 's complexity and performance issues_

Is Kotlin really faster than Scala?

~~~
grabcocque
Scala's build times are legendarily horrible. Kotlin has similar compile times
to Javac.

~~~
pjmlp
They are working on improving them though, specially with Dotty.

In any case they aren't as bad as C++ (at least until modules arrive).

~~~
smitherfield
_> In any case they aren't as bad as C++_

Speaking as a fan of Scala, I find that scalac takes a noticeable amount of
time even to compile trivial Hello World-type programs. Huge C++ codebases
like Chromium or Boost take ages to compile, but I seriously doubt a
comparable Scala codebase would be faster.

~~~
pjmlp
I only dabbled a few times in Scala, but I do regularly use C++ on some side
projects.

Do Scala compilation times get measured in hours?

Just the other day I gave up compiling Cocos2D-X, after it crossed the one
hour threshold on my humble dual core, 8GB.

~~~
smitherfield
I also haven't used Scala at scale enough to say; the incremental compilation
story is much better than templated C++ but I think compiling a large codebase
from scratch would be quite slow, and slower than templated C++.

I think any design for generics that provides the same guarantees as C++
templates (type-safe, resolved at compile-time, value types aren't boxed) is
necessarily going to cause long compilation times. The only other
implementation I know of that checks all three boxes is Rust's, and so far
generic Rust is even slower to compile than templated C++.

~~~
pjmlp
> I think any design for generics that provides the same guarantees as C++
> templates (type-safe, resolved at compile-time, value types aren't boxed) is
> necessarily going to cause long compilation times.

This isn't true of Ada, Modula-3 or Eiffel generics.

As for Rust, yes their compilation story still needs some improvements, but
they still need to enable incremental compilation and cargo is yet to be able
to handle binary dependencies across projects.

Where I suffered more in C++ compilation times was building the latest
versions from either Swift or Rust, LLVM just takes ages to compile.

Thankfully I mostly use C++ as helping hand for Java/.NET.

~~~
smitherfield
_> This isn't true of Ada, Modula-3 or Eiffel generics._

I'm not familiar with those languages, but it appears that Eiffel and Modula-3
generics only work with boxed (implemented as pass-by-reference) types, and
Eiffel doesn't have pass-by-value types at all.

It does look like Ada generics fulfil those three conditions.

~~~
pjmlp
In Eiffel, a value type is known as an _expanded_ class and it can be given as
default class attribute, or when declaring a variable. So generic
instantiation will do the right thing depending on the class attributes.

Modula-3 supports value types just like any Algol language, you just need to
provide the types you want when instatiating a generic module interface.
Modula-3 generics work at module level.

Here is an example for numeric types, with a generic module for all numeric
operations:

[https://github.com/modula3/cm3/blob/d31f6ca184c66755317ba990...](https://github.com/modula3/cm3/blob/d31f6ca184c66755317ba990a2bd6fb2f0af7f0c/m3-libs/m3core/src/word/GenWord.ig)

Specialized to LONGINT.

[https://github.com/modula3/cm3/blob/d31f6ca184c66755317ba990...](https://github.com/modula3/cm3/blob/d31f6ca184c66755317ba990a2bd6fb2f0af7f0c/m3-libs/m3core/src/word/Long.i3)

[https://github.com/modula3/cm3/blob/d31f6ca184c66755317ba990...](https://github.com/modula3/cm3/blob/d31f6ca184c66755317ba990a2bd6fb2f0af7f0c/m3-libs/m3core/src/word/LongRep.i3)

It is a bit convoluted, but it isn't easy to track down a simple example.

------
mwcampbell
I wonder how Kotlin's JavaScript backend compars to Scala.js in Li Haoyi's
list of fundamental reasons for betting on Scala.js:

[http://www.lihaoyi.com/post/FromfirstprinciplesWhyIbetonScal...](http://www.lihaoyi.com/post/FromfirstprinciplesWhyIbetonScalajs.html)

I have a few concerns about Kotlin's JS backend:

How much can the complete JS bundle be optimized by an advanced whole-program
optimizer like the Google Closure Compiler?

Kotlin for JS has no reflection, and I think JetBrains should _not_ look into
that as they said they are. But is there powerful compile-time code generation
instead, e.g. through annotation processors?

Is there, or will there be, a healthy ecosystem of pure-Kotlin libraries that
can be used with both the JVM and JS?

~~~
bashor_
> How much can the complete JS bundle be optimized by an advanced whole-
> program optimizer like the Google Closure Compiler?

JavaScript backend generates code understandable by static analyzers so it can
be optimized. Anyway, we going to continue works in this area in near future.

> Kotlin for JS has no reflection, and I think JetBrains should not look into
> that as they said they are. But is there powerful compile-time code
> generation instead, e.g. through annotation processors?

Why do you think that reflection is a bad idea? Honestly, we don't have a
final decision about it, it requires investigations and discussions. Note it's
very important for us to have an ability to provide good tools (e.g IDE
support) for features.

> Is there, or will there be, a healthy ecosystem of pure-Kotlin libraries
> that can be used with both the JVM and JS?

We will work on it.

~~~
mwcampbell
> Why do you think that reflection is a bad idea?

OK, reflection isn't necessarily a bad idea. Clearly development tools need
it, including future tools that run in the browser (e.g. an in-browser IDE).
But I think that applications that are not development tools should not use
reflection, because it interferes with static optimization, as the article
that I linked in my original comment explains. So most libraries should
probably not use reflection either. That means we need an alternative to
reflection. For example, chapter 10 of _Kotlin in Action_ uses reflection to
implement a serialization library. I think it would be better to use build-
time code generation for that, even outside the JS use case (e.g. reflection
interfers with optimization of Android app packages with ProGuard).

Edit: By the way, thanks for taking the time to respond.

------
danneu
For fun, I've been working on a simple hobby web server in Kotlin that wraps
Jetty: [https://github.com/danneu/kog](https://github.com/danneu/kog)

I think Kotlin is the statically-typed language I've been waiting for,
especially with the team's interest in native and JS targets.

~~~
Scarbutt
Do you have any opinions on Kotlin vs Clojure? are you liking Kotlin more?

~~~
danneu
I think static typing + solid IDE support is too useful to pass up, and that's
what Kotlin has.

But Kotlin also has simplicity that, for example, Scala doesn't. I tried
Clojure after spending a couple months with Scala and giving up, and then I
used Clojure fulltime for 3+ years.

Eventually, I felt like I was hitting a ceiling with Clojure since it lacked
good static analysis. I moved to Node because I felt like, if I'm going to use
a dynamically-typed language, I might as well use the ubiquitous one. Also,
Clojure was a hard sell to other developers I would meet.

What Clojure does have is an ecosystem of simplicity that I hope the Kotlin
ecosystem will adopt.

For example, compare ztellman's [http://aleph.io/](http://aleph.io/) (Clojure)
to Java's Netty or to the somewhat mind-numbing
[https://docs.oracle.com/javase/8/docs/api/java/util/concurre...](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletionStage.html).
And compare Clojure's Ring to any Java web framework.

Even Spark, Java's simplest option afaict, doesn't have real middleware, just
before/after "filters". That's why I went with the `(Request) -> Response`
signature over the `(Request, Response) -> Unit` signature.

There's also ClojureScript's integration and tooling which are pretty
impressive these days while Kotlin's JS support is still fledgling.

Though these are the impressions of someone who has only been using Kotlin for
a few months without a Java background. And it's not like anyone should be
using my lil framework in production, it's just a hobby.

~~~
cutler
Clojure and Kotlin are, for me, the brightest stars in the JVM sky. However, I
fear Clojure adoption has peaked at its current low rate probably because it
was never aimed at the corporate Java world as Kotlin is. Clojure appeals more
to the software artisan which is a valuable niche in itself but its syntax,
which is its strength, is so different from Java's that I think it is rarely
seen as an alternative to Java.

------
dep_b
I wouldn't touch Java if I could write something in Kotlin instead. I'm using
Swift on a daily base and while it still has it's issues (compile times,
breaking changes) it's a great language to work in and extremely similar to
Kotlin.

Better type safety means less crashes.

------
meddlepal
I've been very happy writing Kotlin backend code for the last fourteen months
or so.

Definitely excited. It's an awesome JVM language.

~~~
agumonkey
I'm investigating Kotlin. Do you have references, blogs etc to suggest ?

~~~
Larrikin
Through this blog
[https://antonioleiva.com/kotlin/](https://antonioleiva.com/kotlin/) And his
book I was able to become fairly productive in the language in only a few
weeks. I was familiar with Scala however. The automatic conversion of java to
Kotlin in Intelli J's IDEs also helped immensely.

------
afastow
The post specifically mentions using Kotlin Javascript to develop React
applications. Are there any simple examples out there showing this? I'm not
seeing any after a quick google search and look through the docs.

Developing a modern frontend app is complex enough with Javascript or
Typescript. It's pretty hard to ask people to figure it out from scratch in
Kotlin by just saying that it's possible but not showing how.

~~~
Teedee753
It was mentioned on the Kotlin slack that Jetbrains would make some examples
available within a few weeks on using Kotlin JS including using it with React.

------
netcraft
kotlinjs is very interesting to me - anyone have experience with writing
kotlin and typescript and can compare?

------
curyous
Why couldn't they have used "func" for functions, like so many languages do,
instead of "fun"?

~~~
richard_todd
I don't think of 'func' as a standard. Off the top of my head, you can see
"defun", "fn", "def", "function", and on and on in various languages. I don't
see much agreement across the board.

~~~
netcraft
does anyone know of a place that compares syntax like this across languages?
I've found myself often wondering if there is consensus for certain features
or syntax.

~~~
chocolateboy
[http://rigaux.org/language-study/syntax-across-
languages.htm...](http://rigaux.org/language-study/syntax-across-
languages.html#FnctnFnctDfnt)

------
jbmorgado
Does this mean we can now build a full React Native app using only Kotlin?

~~~
bashor_
We going to publish more materials about JavaScript in the next few weeks.
Including with using React.

~~~
swsieber
Will there be anything about angular?

Also, is it possible to set up kotlin source code in gradle to compile both to
JS and the JVM at the same time? I'm specificially thinking that it'd be nice
to "share" code between front and backend for some logic and data model
definitions.... I think that'd be a killer feature.

~~~
vorg
If you can, make sure you write your Gradle build scripts in Kotlin also
(possible since Gradle 3.0 was released 6 months ago) instead of Apache
Groovy, then Kotlin will be the only language you need for anything!

