views:

90

answers:

3

I use Java where we only have packages. I know there are other programming languages that also include modules.

What's the difference?

Thanks

+5  A: 

It's hard to compare semantics in the void. (What other languages do you mean?) A "module" might be analogous to a Java class, or a Java package, or something else entirely, depending on that other language. Typically since "modules" tend to be from procedural languages, I'd lean toward saying Java class, but I think the line is very fuzzy at that point and you could argue package quite convincingly.

T.J. Crowder
Ruby comes to mind, also, I've heard Java7 will include "modules"... , but in general terms what would be ( if any ) the difference. For instance, in procedural world, what is a module?
OscarRyz
@OscarRyz: Yeah, there was talk, don't know if anything ever came of it. And of course, that's assuming there *is* a Java7... ;-)
T.J. Crowder
@OscarRyz: Re your "what is a module" question -- it really depends on what procedural language you're talking about. By and large, they're symbol-scoping and -packaging mechanisms, which of course Java classes and packages both are as well.
T.J. Crowder
A: 

A package is more akin to a C++ namespace than a module. A module is more akin to an enclosing class than to a package.

EJP
A: 

instanceofTom's comment nailed it - Different languages have different definitions of package and module. Therefore there's no language agnostic answer to this question.

I'll try to answer it from the perspective of some of the languages I know:

  • Java: It has a concept of packages, which are basically just a mechanism for organizing Java classes, interfaces etc into namespaces. They require hierarchical structure. packages don't have first class status. It might also be worth noting that superpackages proposed for Java 7 are also sometimes referred to as modules.

  • Modula: modules, same in concept to Java's packages. Don't require hierarchical structure.

  • C#: namespaces, same in concept to Java's packages. Don't require hierarchical structure.

  • C++: namespaces, As the name indicates, these are just namespaces. Don't require hierarchical structure.

  • Haskell: modules, same in concept to Java's packages.

  • Scala: packages in Scala are same as packages in Java, except that they don't require hierarchical structures. A few more restrictions like one-public-class-per-file have also been relaxed. objects in Scala are also referred to as modules, and they also enjoy the first-class status.

  • F#: namespaces in F# are same as C# namespaces. Besides namespaces, F# also has modules which are implemented at CLR level as .NET classes with static methods. They aren't first-class entities.

missingfaktor
Experts, please correct me if any of the details is wrong.
missingfaktor