2

By some reasons I need to use long names of the packages in my project. Like:

com.example.foo.bar.bazz.anotherlongsubpart.andfinallytheactualpackagname

So considering the name convention(at least what I know about name convention in Java), I have to create appropriate directory structure replacing dots with slashes:

com/example/foo/bar/bazz/anotherlongsubpart/andfinallytheactualpackagname

As for me it looks too ugly :) So I tried instead to replace with slashes only the part of dots:

com.example.foo.bar.bazz.anotherlongsubpart/andfinallytheactualpackagname

In other words I have here one package/directory with long name "com.example.foo.bar.bazz.anotherlongsubpart" and another one with "andfinallytheactualpackagname". Is it legal to do that? I tried to compile all this stuff using Simple Build Tool, and run my unit tests on it. And it works well. As I would name it in more ordinary fashion with slashes.

So as my experiment shows it works well at least with SBT. But I'm not sure whether such practice may lead to further issues? Maybe in different environments with different kind of class loaders etc?

Ilya Lakhin
  • 1,904
  • 1
  • 16
  • 31

3 Answers3

8

In Scala, the directory name does not have to map to the package declared at the source file, the compiler won't enforce this in any way.

In Java, you can't do that. The source file must be at the correct directory (as dictated by the package name declared a the source file) for it to compile.

So, if you're in Scala, you can do it as much as you like, in Java, no, you can't.

Maurício Linhares
  • 39,901
  • 14
  • 121
  • 158
4

Scala doesn't care what your directories are named, Java does. If you have Java code, follow its conventions. For Scala, do whatever you feel like (including not using package name for directories).

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
3

I would not recommend doing that. As discussed in the Oracle package name documentation Many tools that need to translate between qualified class names and directory locations will simply replace "." with file separators and your names would break that.

dkatzel
  • 31,188
  • 3
  • 63
  • 67