What's happening
If the scala parameter is an existing .scala file, it will be compiled in memory and run. When there is one top-level object, the main method is searched and, if found, is executed. If this is not the case, the top-level operators are wrapped with a synthetic main method, which will be executed instead.
This is why deleting top-level QSort objects allows you to run your main method.
If you are going to deploy this to the full program, I advise you to compile and run (use a build tool, for example sbt ) the compiled .class files:
scalac main.scala && scala MainObject
If you are writing a single script file, just release the main method (and its object) and write the statements that you want to execute in the outer scope, for example:
// qsort.scala object QSort{ def apply(array: List[Int]):List[Int]={ array } } val unsorted = List(8,3,1,0,4,6,4,6,5) print("hello" + unsorted toString) val sorted = QSort(unsorted) sorted foreach println
and execute with: scala qsort.scala
Small context
The scala command is designed to execute both scala "scripts" (single-file programs) and complex java-like programs (with the main object and a bunch of classes in the classpath).
From man scala :
The scala utility runs Scala code using a Java runtime environment. The Scala code to run is specified in one of three ways: 1. With no arguments specified, a Scala shell starts and reads com- mands interactively. 2. With -howtorun:object specified, the fully qualified name of a top-level Scala object may be specified. The object should pre- viously have been compiled using scalac(1). 3. With -howtorun:script specified, a file containing Scala code may be specified.
Unless explicitly stated, howtorun mode howtorun guessed from the arguments passed to the script.
If the full name of the object is given, scala guesses -howtorun:object and expects a compiled object with that name in the path.
Otherwise, if the scala parameter is an existing .scala file, it is assumed that -howtorun:script and the entry point are selected as described above.