Kotlin & JavaScript

Vienna Kotlin Meetup, 13. 9. 2017

Today's agenda

  • A Bit of Theory
  • Demo Project
  • Learnings & Outlook

Motivation

Javascript is nice

But Kotlin is nicer

Javascript community:

Use the same language in the frontend and the backend (with node.js).

(Future) Kotlin community:

Let's do the same!
And also add native support!
  • Common programming language
  • Share code
    • Structures like DTOs
    • Business or Domain Logic
    • Libraries

Kotlin & JS

Core: Transpiler & Tooling

Transpiling

Kotlin:


fun main(args: Array<String>) = println("Hi Kotlin Vienna!")
							

Javascript:


function main(args) {
    console.log('Hi Kotlin Vienna!')
}
							

How does it work in reality?

  • kotlin.js
  • Gradle Plugin
  • Integration into Webpack

Calling Javascript functions from Kotlin


js("console.log(3+4)")
						

Defining JavaScript structures


external class Window {
	fun alert(message: String): Unit
}

external val window: Window
						

external class Window {
	fun alert(message: String): Unit
}

external val window: Window

external fun unknownReturnType(): dynamic
						

Dynamic examples


val dyn: dynamic = unknownReturnType()
						

dyn.whatever(123)
						

dyn.foo.bar[4].whatever(123)
						

Calling Kotlin from JS


fun foo(person: String) = "Hi $person"
					

console.log(MyModule.foo('Martin'));
					

console.log(require('myModule').foo('Martin'));
					

@JSName for explicit naming

Reflection

Very limited

Just "simpleName" and "isInstance"

No annotation support!

Demo Time!

Next steps

  • Framework support: Not yet
    • Manual header creation
    • ts2kt
  • CSS support: In progress

Conclusion

Architecture is right but not implemented (yet).

Documentation for language support is good.

Documentation for tooling isn't.

Don't migrate (yet)!

Don't use in production!

But try it in your next pet project!

Slack channels: #javascript & #multiplattform