So I know Kotlin Native is obviously Native and Kotlin JVM isn't but is the code between Kotlin JVM and Kotlin Native:
1. Different Compiler and Different Code
2. Different Compiler and Similar Code
3. Different Compiler and Same Code
4. None of the above (please explain)
The Kotlin/JVM and Kotlin/Native compilers share the front-end (the part that performs code parsing, name resolution, type inference etc.), but the compiler back-ends that translate the internal program representation to the target code (the JVM bytecode and LLVM bitcode, respectively) are different.
The Kotlin language accepted by the two compilers is the same, but some of the features and checks are platform-specific. Also, the standard libraries for Kotlin/JVM and Kotlin/Native are sufficiently different, see the APIs available on each platform here: Kotlin Standard Library.
Another big difference is the memory model: Kotlin/JVM uses the Java memory model, while Kotlin/Native offers its own concurrency and memory model.
Also, the dependencies that one can use in Kotlin/JVM and Kotlin/Native projects are different. In addition to the projects built using the same Kotlin target:
Kotlin/JVM can also use any libraries built for the JVM (written in Java, Scala etc.)
Kotlin/Native can also interoperate with native libraries written in C (or at least having C headers) using the C interop tools.
Both Kotlin/JVM and Kotlin/Native can use Kotlin Multiplatform libraries. Given that a dependency is a multiplatform library, one can entirely reuse the code working with it between Kotlin/JVM and Kotlin/Native.
Related
I want to create a Kotlin project compatible with Android and Desktop. This project needs to bind to a C library.
The way I understand it, I should create a Kotlin multiplatform project, and have a common code which wraps the C library using JNI.
However, Kotlin/Native allows a way easier interop with C libraries, so I'd like to use that. But it seems like Kotlin/Native is a platform (equal to eg jvm or android), so it can't be used as a common code.
Is there a way to do what I want? I couldn't find any simple example doing that.
If that's not possible, why? Kotlin/Native is able to target desktop and android platforms. If it's possible to use Kotlin/Native on Android, why is it impossible to use a Kotlin/Native library from a "normal" desktop/android project?
This should be possible with Kotlin Multiplatform, by having a native target.
See https://kotlinlang.org/docs/native-app-with-c-and-libcurl.html#create-a-definition-file
Kotlin/Native helps consume standard C libraries, opening up an entire ecosystem of functionality that exists for pretty much anything you may need. Kotlin/Native is already shipped with a set of prebuilt platform libraries, which provide some additional common functionality to the standard library.
UPDATE:
"common" code in Kotlin Multiplatform under the hood either:
a) has platform-agnostic Kotlin code or
b) uses expect/actual to define platform abstractions
(AFAIK)
Since a C library isn't a), you'll have to define the actual platform definitions, ending up with JNI.
TL;DR KMP isn't suitable for what you're trying to do
I read the Big Nerd Ranch guide to Kotlin and it talked in several places about Kotlin/Java interop, but never JS or native. I already had a solid background in Java, so I have gotten used to using Java classes in my Kotlin code. I am trying to write a Kotlin program which will be run on a site where most - if not all - functionality is written in JavaScript, and I am trying to understand how to write my code to make sure that it is interoperable. Will I be able to continue using Java classes in my Kotlin/JS code? What are the differences between writing Kotlin/JVM code and Kotlin/JS code? What should a (ex-) java programmer know when learning to interop with JS using Kotlin? If there are a few chapters on this in any good books written in the recent past, that would be helpful also.
As Steve already mentioned, you can't utilise java classes in Kotlin/JS.
Think of Kotlin/JS as Typescript.
It provides a different syntax to write code that ultimately compiles to JS.
Here are the notable differences of writing Kotlin/JS code vs Kotlin/JVM code
Kotlin/JS internally uses yarn for dependency management. This enables you to depend on any javascript module available on npmjs etc (see note below)
In addition to standard library, you can also leverage other kotlin-first frameworks such as kotlinx-serialization, ktor etc
Testing libraries will be JS specific. So instead of mockito / mockk / junit family, you'll need to get familiar with karma / mocha family.
There will be a difference in Coroutine capabilities - both in terms of the way one writes code and performance expectations.
I found reading about Kotlin Multiplatform helped clarify a lot about the capabilities of kotlin.
I know this was not specifically asked, but giving my 2cents to people considering Kotlin/JS (as of Sep'20)
Great if you're familiar with Kotlin and don't foresee too many third party dependencies apart from http i/o (ktor) , React ( kotlin-react) and basic html / css (covered by kotlin-styled).
Using JS modules as dependencies is not as straight forward as using JVM dependencies since there is no ready-made interop. One has to define javascript functions/classes in kotlin before using them (see here). So if you foresee leveraging a lot of existing javascript modules, it won't be an ideal way forward.
Great if you have a typical backend-frontend model where backend compiles to JVM and Frontend compiles to JS. You can leverage a common data model and http i/o framework across Backend and Frontend code (via Kotlin Multiplatform). I've found this to be a tremendous productivity boost!
Kotlin/JS compiles Kotlin code, including its own standard library, into Javascript code. At the end, that's all you have is Javascript. What you don't have is any connection to the Java Virtual Machine. Kotlin's standard library provides no magic to bridge Javascript code to the JVM so that it can utilize Java classes. So NO, you can't utilize Java classes in standard Kotlin/JS.
The official Kotlin/Native documentation states that Kotlin/Native
.. is an LLVM based backend for the Kotlin compiler.
As far as I understand:
The Kotlin compiler (kotlinc) produces .class files (with Java bytecode) from your Kotlin source files.
Generic LLVM backends (that are not related to Kotlin) take LLVM IR and turn it into binary code.
Therefore, is Kotlin/Native converting Java bytecode into LLVM IR?
If so, is it correct to state that Kotlin/Native is a LLVM backend? Does Kotlin code get compiled into LLVM IR?
If not, what is the input and output of each compilation step? (e.g.: Kotlin -(kotlinc)-> Java bytecode -(LLVM backend-> native binary)
This blog post states that the Kotlin frontend compiler (I think it is referring to kotlinc) produces Kotlin IR, which I have never read of.
Kotlin compiler has a single front-end but multiple back-end depending upon the the plugin you are using to build the code. Kotlin/Native plugin converts the Kotlin Intermediate Representation (IR) to native code (i.e code that is machine executable).
Is this quote correct?
It tells you that the compilation process is the same for Java bytecode, native code and JavaScript. You compile your Kotlin code and then you have 3 backend compilers that deliver the expected output format (Java bytecode, JavaScript, binary code).
Does the final platform specific binary include the native Kotlin standard library or is it linked dynamically?
Kotlin compiler has a single front-end but multiple back-end depending upon the the plugin you are using to build the code. Kotlin/Native plugin converts the Kotlin Intermediate Representation (IR) to native code (i.e code that is machine executable).
Is this quote correct?
Yes, it's quite correct. The intermediate representation (IR) is a form that universally represents a Kotlin program, regardless of the platform. Then a platform-specific back-end translates the IR to the final binary form – the JVM class files for Kotlin/JVM, LLVM bitcode and other metadata packed into a *.klib for Kotlin/Native.
So, Kotlin/Native doesn't deal with the JVM bytecode in any way.
The intermediate representation is an implementation detail of the Kotlin compiler, and it's not used by any other consumer than the Kotlin compiler itself, so it's not a thing that a developer should care about. For the JVM, it's even kept in memory and never written to binaries. For Kotlin/Native, it's actually serialized and written into the *.klib to be reused.
It tells you that the compilation process is the same for Java bytecode, native code and JavaScript. You compile your Kotlin code and then you have 3 backend compilers that deliver the expected output format (Java bytecode, JavaScript, binary code).
Actually, a part of the compilation process is the same, namely, the front-end language analysis, which includes parsing, call resolution, type inference, diagnostics and other steps that don't require any code transformations that are platform-specific. Even so, the front-end part is tuned for each of the platforms, as those allow different sets of language features and provide different sets of diagnostics.
And the last question does the final platform specific binary include the native Kotlin standard library? Or is it linked dynamically?
Currently, the standard library is statically linked into the resulting Kotlin/Native binaries.
I've read the documentation on .NET programming in C++/CLI, and understand at a high level that it can compile C or C++ to .NET MSIL. I also understand that native structures are translated in the process.
The question is, can I compile a C/C++ codebase into a .NET Portable Class Library using C++/CLI? The intention is to use the result across various platforms, e.g. the Xamarin platforms and UWP.
Edit: Is it easier to do this for plain C, rather than C++?
Short answer: AFAIK no.
Long answer:
As far as I know the C++/CLI source code is compiled in "mixed" mode. It means that if you learn C++/CLI language and create managed classes with it, they run in .NET natively. That's good. But if you simply take your existing C++ code and compile it, the result is the native x86/Windows code, which cannot be used on other platforms. It is called "mixed" because the compiler puts native and .NET IL code together to single executable file.
C++/CLI is usually used in situations where you want to use the existing C++ code
as a part of .NET program in Windows. So you create a library in C++/CLI and create an interface for it in managed C++/CLI. This managed C++/CLI interface is a bridge between native C++ code and the rest of your program in .NET.
Also, as far as I know, C++/CLI is generally not supported by CoreCLR.
There are lots of oop languages, but I couldn't find any that has conveniences like garbage collection, but compiles natively to machine code. Sort of like between C and java/c#. One interesting language I found was Vala, but that's limited to the GNOME platform and is not that well-known
Go is probably closest.
But why on earth do you want it natively compiled anyway?
JIT compilation of portable bytecode has proved to be an extremely effective strategy. It compiles down to native code at runtime (so you get up to the performance of native code after the first few iterations) and it avoids the issues of having to build and manage platform-specific compiled binaries.
Are you thinking about C++? That is in high usage and can be compiled on nearly any (major) platform.
In the case you want to use an oo language that compiles down to native code you will "always" have to use header files and stuff as the elf format doesn't support oo (There is no class information in elf).In case you want to use classes from external libs you need to make the compiler aware somehow about the fact that there are classes, functions, etc. that are declared outside of your project. In C++ this is solved by the use of header files. So that's, I think, a major drawback in native object oriented languages. To resolve that issue a few tweaks would need to be made to elf/loader/linker in order to support the kind of features (like "linking" against "classes") you might expect. Even though mechanism for garbage collection could be implemented even in native languages. But that seems no good for os implementation.
There are C++ libs to do that for userspace apps like:
Boehm collector
Smart pointers