docs.scala-lang icon indicating copy to clipboard operation
docs.scala-lang copied to clipboard

Main methods: Description is different to behaviour on Windows 11

Open gttfrdggr opened this issue 3 years ago • 0 comments

Hello folks, I followed the description exactly (!) of the "happyBirthday" example on https://docs.scala-lang.org/scala3/book/methods-main-methods.html. My PC runs Windows 11. Quick summary: I put the mentioned source code into a file called Main.scala: " PS D:\scala\nochntest> more Main.scala @main def happyBirthday(age: Int, name: String, others: String*) = val suffix = age % 100 match case 11 | 12 | 13 => "th" case _ => age % 10 match case 1 => "st" case 2 => "nd" case 3 => "rd" case _ => "th" val bldr = new StringBuilder(s"Happy $age$suffix birthday, $name") for other <- others do bldr.append(" and ").append(other) bldr.toString " called the compiler (scalac Main.scala) and ran the program. Surprisingly, I got the following error message instead of the described output: " PS D:\scala\nochntest> scala happyBirthday 23 Lisa Peter Illegal command line: java.lang.NumberFormatException: For input string: "happyBirthday" " The problem - as far as I understand it - is that the run time environment hands over the program name as argument no. 0 (zero) to the program to run. When I checked this, I got additionally a quite surprising result, namely: the run time environment hands over to the program to run not only the program name in the first place, but it seems also to hand over the actual arguments TWICE. Please see the following steps to reproduce the issue: Put the following code into a file called "Arguments.scala": " @main def argcount (args: String*): Unit = println(s"Hello world! Here are my ${args.size} arguments...") for arg <- args do println(arg) " Call the compiler and run it, I could witness the following behaviour on my PC/ Windows 11: " PS D:\scala\nochntest> scalac Arguments.scala PS D:\scala\nochntest> scala argcount a1 a2 Hello world! Here are my 5 arguments... argcount a1 a2 a1 a2 PS D:\scala\nochntest> " I am surprised, that the run time environment seems to double the provided arguments and appending them to the program name as very first arg. - this seems to constitute the arguments list available to the program.

One can argue, if this is an issue of the documentation or an issue of the implementation of the run time system of Scala 3. I thought, because the "thing" is not crashing but is providing some (somehow) meaningful behaviour, it could be the documentation which should reflect the (somehow) strange behaviour.

If you feel differently, please forward the issue to the implementors....

Last remark: I didn't check on Unix/ Linux systems or former Windows releases...

Thank you very much for the nearly always very informative documentation - I am using it extensively.

Best wishes, Gottfried

PS. Last remark - your example congratulation program should print the string out, otherwise one wouldn't get the envisaged output at all. Possibly you could test all your program snippets before publishing these :-)

gttfrdggr avatar Apr 01 '22 21:04 gttfrdggr