kingluo
kingluo

Reputation: 1785

scala: relationship between package and source directory hierarchy

Does the package declared in the source file must match the directory hierarchy? For example, in source file a/b/c/foo.scala, the package must be a.b.c? If so, why bother to repeat again in the source file? If not, since the source file could declare any package it belongs to, then what's the usage of the directory hierarchy?

And is there some restriction about what could be inside the source file? For example, does the foo.scala could contain public class foo, and other classes or functions must be private or package access only?

Upvotes: 3

Views: 376

Answers (1)

Ren
Ren

Reputation: 3455

Its not a requirement, but it the usual behaviour. A file a/b/c/foo.scala could be package x.y.z. I suppose you could say directories manage the way you store your code, and packages manage the way you expose your code.

There are no restrictions about what can be inside the source file (This is different to Java). foo.scala could contain the public class bar, or foo, or both, for example.

Upvotes: 3

Related Questions