-1

I am trying to participate in online Codeforces contests using Kotlin.

My understanding is I should use Kotlin script if my code is contained within a single file.

If I run the following file locally (version 1.6.10):

kotlin just_main.main.kts
// just_main.main.kts
fun main() {
    println("Hello World")
}

Nothing happens. I need to add an explicit call for it to actually execute main:

// top_level_call.main.kts
fun main() {
    println("Hello World")
}

main()

So far, so normal. The problem occurs when I try to submit my solution to the Codeforces online judge. The judge expects no top-level code and runs the main function instead. So just_main runs fine, but top_level_call produces a compilation error:

Can't compile file:
program.kt:43:1: error: expecting a top level declaration

main()

^

This leads to the awkward situation of me having to add the main() call when I want to try my solution locally, but having to remove it every time I upload an attempt.

Is there a way to have my local Kotlin behave the same as the online judge, meaning implicitly running any main functions (meaning just_main would produce output)?

xjcl
  • 12,848
  • 6
  • 67
  • 89
  • AIUI, Kotlin script is not just about keeping all the code in one file; it's also about simpler distribution and changes and execution, at the cost of time taken to recompile before every execution. That has some knock-on effects on the way programs are written, as you've discovered! – gidds Mar 10 '22 at 18:28
  • My sad workaround is now coding my solution in a `.kts` file (because unlike `.kt` files it has proper highlighting and intellisense in IntelliJ), then running a bash command which copies it to a `.kt` file then compiles and runs it. – xjcl Mar 11 '22 at 13:04
  • I think a good number of us here could attest to IntelliJ having proper code highlighting, formatting, completion, refactoring, &c for `.kt` files! If you're not getting that, then there's likely to be a problem with the way your project and/or IntelliJ is set up. (For example, is your file in a designated source folder known to IntelliJ, and/or to Maven/Gradle if those are used?) – gidds Mar 11 '22 at 18:30
  • I am using my competitive programming folder as the project. A path to an individual file looks like `./2022/cf_1647__777_2/a.kt`. My hypothesis is that IntelliJ can't deal with a project made up of independent individual scripts and expects some sort of Maven/Gradle integration. – xjcl Mar 11 '22 at 22:00
  • If I might add: When hovering over variable definitions, it gives the type of `ERROR` in .kt while it shows the correct type in .kts -- no idea why. – xjcl Mar 12 '22 at 21:17

1 Answers1

0

I haven't found a way to do this with Kotlin script files, but you can also use normal .kt files without having any classes in the file (my understanding is that Kotlin magically turns them into Java class bytecode/files):

kotlinc a.kt && kotlin AKt < a.in

This "runs" a.kt with standard input from a.in.

(And yes I only found this after I already wrote the question)

xjcl
  • 12,848
  • 6
  • 67
  • 89