File tree Expand file tree Collapse file tree 4 files changed +39
-0
lines changed Expand file tree Collapse file tree 4 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -72,4 +72,5 @@ with easy interaction with Kotlin code and can be executed:
7272 ```
7373 kotlin kotlin-shell.main.kts main-kts/example.txt
7474 ```
75+ - [ ` composite/composite.main.kts ` ] ( scripts/composite/composite.main.kts ) demonstrates script composition capabilities via ` @file:Import ` annotation
7576
Original file line number Diff line number Diff line change 1+ #! / usr/ bin/ env kotlin
2+
3+ /* *
4+ * A script demonstrating the composition functionality of .main.kts scripts using imports.
5+ *
6+ * Key features:
7+ * - Supports both relative and absolute paths for imports
8+ * - Multiple imports using repeated annotations
9+ * - All public top-level declarations (variables, functions, classes) from imported scripts
10+ * become available in the importing script
11+ * - Each imported script is evaluated when imported, not just its declarations are included
12+ * - Imported scripts are evaluated in the order they appear
13+ *
14+ * Note: Import resolution is performed relative to the importing script's location
15+ * when using relative paths.
16+ */
17+
18+
19+ @file:Import(" kts/simple.kts" )
20+ @file:Import(" imported.main.kts" )
21+
22+ sharedMainKtsVar++
23+ println (" simple.kts members: $ktsScriptClassMembers " )
Original file line number Diff line number Diff line change 1+ var sharedMainKtsVar = 2
2+
3+ // The __FILE__ variable contains the absolute path of the current .main.kts script
4+ println (" Hi from $__FILE__ " )
Original file line number Diff line number Diff line change 1+ import kotlin.reflect.full.declaredMembers
2+
3+ // Kotlin scripts (.kts files) automatically include these dependencies:
4+ // 1. kotlin-script-runtime.jar - provides script-specific functionality
5+ val scriptArgs = args
6+
7+ // 2. kotlin-stdlib.jar - provides the Kotlin standard library
8+ println (" Hello from simple.kts! Args=$scriptArgs " )
9+
10+ // 3. kotlin-reflect.jar - provides reflection capabilities
11+ val ktsScriptClassMembers = this ::class .declaredMembers
You can’t perform that action at this time.
0 commit comments