Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would a Kotlin notebook work when kotlin is a compiled language?

I tried the Kotlin notebook extension and I wonder what is going under the hood.
I have used notebooks with python and because python is an interpreted language it seems natural that it works in the notebook.

Kotlin notebook

My understanding is that after writing the Kotlin code I need to compile it (No?) before I execute it. But in notebook, if I create a class in one cell, I am able to instantiate an object in the next cell and I can't figure out how is this working.

EDIT: It appears there are IPYTHON kernels for many of the compiled languages. However the question remains "How do compiled programming languages work in the interactive jupyter notebook environment"?

like image 396
vyi Avatar asked May 08 '18 04:05

vyi


People also ask

Is Kotlin compiled or interpreted?

Yes, when targeting the JVM, Kotlin is compiled to JVM *. class files, which is a bytecode format that can later be either interpreted by a JVM, or compiled to the machine code by the JVM during the program run (JIT), or even compiled ahead-of-time (AOT) down to the machine code.

How does the Kotlin compiler work?

Kotlin lets you choose the version of JVM for execution. By default, the Kotlin/JVM compiler produces Java 8 compatible bytecode. If you want to make use of optimizations available in newer versions of Java, you can explicitly specify the target Java version from 9 to 18.

What language is Kotlin written in?

Kotlin used the Java programming language for its initial implementation, then most of the Kotlin compiler's source code got rewritten to Kotlin.

Is Kotlin compiled to Java?

The Kotlin compiler for JVM compiles Kotlin source files into Java class files. The command-line tools for Kotlin to JVM compilation are kotlinc and kotlinc-jvm . You can also use them for executing Kotlin script files.


1 Answers

Kotlin has a special scripting mode (apart from the ordinary compiled mode), we often name a Kotlin script with the suffix .kts.

You can try this with a command line Kotlin compiler:

  • Write println("hello world") in a.kts
  • Run kotlinc -script a.kts

And you'll see hello world printed on screen.

If you have IntelliJ IDEA, try right-clicking on a .kts file and select Run option. You'll see command line output.

How do compiled programming languages work in the interactive jupyter notebook environment?

There's nothing to do with "compiled" here. This is a special function of Kotlin compiler (running kotlin codes as script).
Not all languages have such feature. This isn't general-purpose.

like image 83
ice1000 Avatar answered Oct 23 '22 12:10

ice1000