If Kotlin for JVM, JS, and Native are to coexist h...
# language-proposals
b
If Kotlin for JVM, JS, and Native are to coexist happily, we're gonna need conditional compiler commands like
#if(JS)
v
Why not create different libraries/modules for different platforms ?
b
Because eventually I'm going to hit a point where I want the same thing on different platforms. Anyone who's built the same app for desktop, mobile, and web knows that 98% of it is identical, and you only really need to adjust for "touch vs mouse" and "large vs small screens". Maybe for "long vs short lifecycle", too, but there's not much else. Why have two entirely separate projects for iOS vs JVM when I could have the exact same one, but abstracted a bit?
The use case that keeps swimming in my head is Java SE's
Graphics2D
and Cocoa Touch's
CGContext
. They're almost identical in API, so I want to write an abstraction layer called
GraphicsContext
that will call branch off to one or the other depending on the OS you compiled it for
v
Maybe it could work with a shared interface (in pure Kotlin) and two implementations in two different modules for two targets
👍 3
It would not even require more code than
#if
Actually even less
b
Alright, what about `typealias`es?
v
What about them?
b
What if, for Kotlin Native, I want to do what Cocoa did with
CGFloat
and have a type that's 32-bit on 32-bit platforms, and 64-bit on 64-bit ones? https://github.com/apple/swift/blob/master/stdlib/public/SDK/CoreGraphics/CGFloat.swift.gyb#L26-L35
v
I have not a slightest idea 😀
p
fwiw, a number of popular scala libraries target both jvm and js
b
It'd be great to see those! I'd love to know how they did it
p
afaik, significant parts of the scala language and stdlib are available on both platforms, so foundational libraries (e.g. cats) may get away with a single codebase.
b
In 1.2 timeframe we going to work on this (https://youtrack.jetbrains.com/issue/KT-6359). Feel free to star or vote the issue to get updates.
😄 1