I’m trying to compile the program with 2 simplest classes:
class BaseClass
placed in BaseClass.scala and
class Test extends BaseClass
placed in Test.scala. Issuing command scalac Test.scala fails, cause BaseClass is not found.
I don’t want to compile classes one by one or using scalac *.scala.
The same operation in java works: javac Test.java. Where am I wrong?
Let’s see first what Java does:
So, as you can see, Java actually compiles
BaseClassautomatically when you do that. Which begs the question: how can it do that? Can does it know what file to compile?Well, when you write
extends BaseClassin Java, you actually know a few things. You know the directory where these files are found, from the package name. It also knowsBaseClassis either in the current file, or in a file calledBaseClass.java. If you doubt either of these facts, try moving the file from directory or renaming it, and see if Java can compile it.So, why can’t Scala do the same? Because it assumes neither thing! Scala’s files can be in any directory, irrespective of the package they declare. In fact, a single Scala file can even declare more than one package, which would make the directory rule impossible. Also, a Scala class can be in any file whatsoever, irrespective of its name.
So, while Java dictates to you what directory the file should be in and what the file is called, and then reaps the benefit by letting you omit filenames from the command line of
javac, Scala let you organize your code in whatever way seems best to you, but requires you to tell it where that code is.Take your pick.