what is the different between Kotlin File/Class and Kotlin Script
Asked Answered
J

2

7

I discovering the programming language kotlin for some weeks, and I always start with new File/Class, and I've noticed lately to kotlin Script in the menu and I ask now what's different between the Script and the normal File in kotlin

Jackdaw answered 8/1, 2019 at 0:10 Comment(0)
S
12

In a Kotlin source *.kt file, you can place top-level declarations, such as types and top-level functions and properties, but not code statements that are not nested into some declarations.

A Kotlin Script *.kts file, on contrary, contains code statements on the top level without any nesting. These statements are an equivalent of a main function. It is allowed to contain types, too, but with some limitations similar to those placed on nested type declarations.

Stuffed answered 8/1, 2019 at 6:27 Comment(5)
Thanks this answer gave me idea about the Subject.Jackdaw
Is there a way to find out what the limitations on Kotlin scripts are? I'm bumping up against some "Expression is inaccessible from a nested class" errors in a kts file and would like to know if it's me or Kotlin.Cysto
For simplicity, I would add that the content of a *.kts file is the actual body of the main function.Diaphragm
Are there any resources to learn more about this topic? Anything is welcomeCarola
@MrCreed, on scripting in general, see kotlinlang.org/docs/…Stuffed
C
0

There are some differences a Kotlin File (.kt) is used for developing structured applications. Contains classes, functions, and other constructs. It’s compiled and run as part of an application. while Kotlin Script (.kts) is mainly used for quick scripts and automation tasks. Kotlin Script allows writing code directly without the need for classes or functions. It's interpreted and executed immediately.

Carthy answered 15/8 at 21:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.