Kotlin 1.2 Released: Sharing Code Between Platforms
blog.jetbrains.com
Kotlin 1.2 Released: Sharing Code Between Platforms
1–10 of 41 posts
Re: Kotlin 1.2 Released: Sharing Code Between Platforms
#2Example from the docs:
// Common module
package org.jetbrains.foo
expect class Foo(bar: String) {
fun frob()
}
fun main(args: Array) {
Foo("Hello").frob()
}
// JVM module
package org.jetbrains.foo
actual class Foo actual constructor(val bar: String) {
actual fun frob() {
println("Frobbing the $bar")
}
}
I brought up how I thought the `expect` and `actual` keywords (but primarily the latter) added unneeded verbosity in the Kotlin Slack awhile back and was pretty much shrugged off, despite folks on the Kotlin team admitting that they exist in part to make the IDE's job easier, but weren't actually necessary.It leaves a sour taste in my mouth, but I suppose it might be because I'm familiar with projects in C/C++ where, if a symbol goes undefined, the compiler/linker is smart enough to tell you that without being hand-held.
Re: Kotlin 1.2 Released: Sharing Code Between Platforms
#3Multiplatform docs here: https://kotlinlang.org/docs/reference/multiplatform.html Example from the docs: // Common module package org.jetbrains.foo expect class Foo(bar: String) { fun frob() } fun main(args: Array ) { Foo("Hello").frob() } // JVM module package org.jetbrains.foo actual class Foo actual constructor(val bar: String) { actual fun frob() { println("Frobbing the $bar") } } I brought up how I thought the `…
I also believe they could have manage it without polluting the code with expect and actual.
Probably the compiler would need some extra work to do the matching.
Re: Kotlin 1.2 Released: Sharing Code Between Platforms
#4Multiplatform docs here: https://kotlinlang.org/docs/reference/multiplatform.html Example from the docs: // Common module package org.jetbrains.foo expect class Foo(bar: String) { fun frob() } fun main(args: Array ) { Foo("Hello").frob() } // JVM module package org.jetbrains.foo actual class Foo actual constructor(val bar: String) { actual fun frob() { println("Frobbing the $bar") } } I brought up how I thought the `…
You are not alone. I also believe they could have manage it without polluting the code with expect and actual. Probably the compiler would need some extra work to do the matching.
Re: Kotlin 1.2 Released: Sharing Code Between Platforms
#5Multiplatform docs here: https://kotlinlang.org/docs/reference/multiplatform.html Example from the docs: // Common module package org.jetbrains.foo expect class Foo(bar: String) { fun frob() } fun main(args: Array ) { Foo("Hello").frob() } // JVM module package org.jetbrains.foo actual class Foo actual constructor(val bar: String) { actual fun frob() { println("Frobbing the $bar") } } I brought up how I thought the `…
Re: Kotlin 1.2 Released: Sharing Code Between Platforms
#6Multiplatform docs here: https://kotlinlang.org/docs/reference/multiplatform.html Example from the docs: // Common module package org.jetbrains.foo expect class Foo(bar: String) { fun frob() } fun main(args: Array ) { Foo("Hello").frob() } // JVM module package org.jetbrains.foo actual class Foo actual constructor(val bar: String) { actual fun frob() { println("Frobbing the $bar") } } I brought up how I thought the `…
It's not really "to make the IDE's job easier", it's to make it explicit that the given class needs to match a certain contract, and to make the validation errors more clear.
Re: Kotlin 1.2 Released: Sharing Code Between Platforms
#7For single target code, you can prefix the class module with the platform tag (e.g. "js" or "java"). These module names are reserved within the compiler and any code therein is restricted to their respective targets.
Another way is to use conditional compilation markers to indicate specific target-compatible boundaries. E.g.
#if java
public function print(s : String) java.lang.System.out.println(v);
#end
That method only exists on the java target, and the compiler will complain otherwise.Conditional compilation also allows for more fine-grained support of multiple targets:
#if java
public function print(s : String) java.lang.System.out.println(v);
#elseif js
public function print(s : String) console.log(v); // as a simple example
#end
This makes a cleaner organizational distinction between extern code that must remain target specific, and true cross platform code that can be called on any of the supported targets. Editors can pick up on this very easily and provide early warnings as you are typing, and doc generators can also inspect these directives and show api availability between targets in the rendered docs.(self-disclosure: Haxe compiler contributor)
Re: Kotlin 1.2 Released: Sharing Code Between Platforms
#8https://kotlinlang.org/docs/tutorials/command-line.html
> "We can download it from GitHub Releases."
https://github.com/JetBrains/kotlin/releases/tag/v1.2
404
Re: Kotlin 1.2 Released: Sharing Code Between Platforms
#9I really like some of Kotlin's decisions, but I prefer Haxe's approach to this. For single target code, you can prefix the class module with the platform tag (e.g. "js" or "java"). These module names are reserved within the compiler and any code therein is restricted to their respective targets. Another way is to use conditional compilation markers to indicate specific target-compatible boundaries. E.g. #if java publ…
And your example shows that there is no way to verify that each module provides a consistent API.
Re: Kotlin 1.2 Released: Sharing Code Between Platforms
#10> "Working with the Command Line Compiler" https://kotlinlang.org/docs/tutorials/command-line.html > "We can download it from GitHub Releases." https://github.com/JetBrains/kotlin/releases/tag/v1.2 404