Well, obviously there is or you wouldn’t get the error you got. If you want to play with code like that, you should probably be using Scastie which has a mode which does what you tried to do.
If you’d rather not depend on an online tool, it depends how you executed it. This isn’t a normal Scala file–you need an object with a main method, at a minimum. But there is a script mode in scala-cli (which is also scala if you have 3.6 and are trying to run the file using scala as a command); if you save your file as .sc and do scala-cli run, it will wrap the code in the appropriate way for the JVM and do what you want.