Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing javascript applications with Kotlin

Tags:

I recently started to have a look at Kotlin and managed to create my first JVM applications. It's so cool to have a single language that compiles both to Java and JS. So, now I started playing with Kotlin2js and tried to understand the Javascript interoperability, and the possibilities to use JS frameworks like jQuery.

I found a couple of blog posts and examples: http://blog.jetbrains.com/kotlin/2013/10/writing-kotlin-in-the-browser/ http://blog.jetbrains.com/kotlin/2014/12/javascript-interop/ https://github.com/JetBrains/kotlin/blob/master/libraries/tools/kotlin-gradle-plugin/src/test/resources/testProject/kotlin2JsProject/mainProject/src/main/kotlin/example/main.kt

Is there a documentation of the Kotlin JS library? It's not yet mentioned at http://kotlinlang.org/api/latest/jvm/stdlib/index.html

I first compiled a simple sample app which used import kotlin.browser.* with gradle as build system which (finally and with some help here worked, thanks again!). Than I imported the project into IDEA and suddenly it didn't compile anymore, I had to change the import to import js.dom.html.*. So, I guess it uses a different version of library? (And IDEA added apply plugin: 'kotlin' to my build.gradle in addition to kotlin2js and I guess this doesn't work.)

IDEA copied kotlin-jslib.jar to lib which says it's "Implementation-Version: 0.7.270" in its Manifest. For the compilation with gradle I used kotlin 1.0.1-1 and I'm pretty sure that I also selected this version in IDEA when creating the project.

So, what are the best sources of information to understand Kotlin2js and the Kotlin-js-lib? Especially the Javascript interoperability, how to use Frameworks like jQuery (it seems that there is jQuery support in the kotlin-js-lib), but also, how I can use other frameworks that don't come with Kotlin support yet. I understood that Kotlin has the dynamic keyword, and http://blog.jetbrains.com/kotlin/2014/12/javascript-interop/ mentioned noImpl which lead to a compile error when I tried to use it. Maybe the best way for now is to look at the Kotlin sources?

Well, this is a rather long and unstructured question covering several aspects, but that's my current state of learning Kotlin :-) And maybe others experience the same problem.

like image 778
Peter T. Avatar asked Mar 27 '16 17:03

Peter T.


People also ask

Can I write JavaScript in Kotlin?

You can freely talk to JavaScript from Kotlin via dynamic types. If you want to use the full power of the Kotlin type system, you can create external declarations for JavaScript libraries which will be understood by the Kotlin compiler and the surrounding tooling.

Can Kotlin replace JavaScript?

But Kotlin can be used for much more than just Android. You can use Kotlin in place of Java in back-end systems. You can even use Kotlin on the front-end, replacing JavaScript as a web language. Kotlin has the potential to be a true full-stack solution, being in use on the server, in the web browser, and on mobile.

Is Kotlin faster than Nodejs?

In general, Kotlin is 5-10x faster than JS and uses 1/2 the memory.

Is Kotlin easier than JavaScript?

Kotlin is way more concise than Java, solving the same problems with fewer lines of code. This improves code maintainability and readability. Kotlin adds a lot of nice-to-have features, a much cleaner syntax, ideas from functional programming, and other enhancements over Java.


1 Answers

Meanwhile Kotlin/JS is available in version 1.2 and there is an introduction, tutorial and reference on the official website.

like image 189
Andi Avatar answered Nov 04 '22 07:11

Andi