Rosetta stone of module syntaxes across languages


#1

I’m trying to compare how packages and modules are declared and defined in programming languages.

The task is:

  • if you have a package/crate/project called “pkg”, define a module/namespace “foo”, in a standalone file used only for that module, containing a function “bar”.
  • use the module within the same package (so that foo.bar()/foo::bar() is callable)
  • use the module from a completely different package (so that foo.bar()/foo::bar() from “pkg” is callable).

How would you organize this? I’ve tried to come up with syntax used in various languages:

^ please view the table in the gist, since Discourse doesn’t support table markup

Please correct me if I got any languages wrong. Feel free to suggest syntax for other languages.


#2

So I went to look to see if this is on Rosetta Code, but either they’re describing this concept in different words or I missed it :rofl:

I found these similar tasks, I’m not sure if this is helpful or not:

If what you’re describing indeed doesn’t exist, I think you should add it as a new task on Rosetta Code!!


#3

Judging from the Rust, CommonJS, and ES6 examples, I gather that the “Use of the module across packages” column is supposed to bring the module into scope under the name “foo”? Then the Python example should be from pkg import foo.

The posted example makes bar available under pkg.foo.bar. Surprisingly, there are in fact cases where import pkg does not accomplish this same feat (namely, when pkg doesn’t import foo, or imports it under another name).

Haskell:

No declarations in the parent module (not counting the extremely long and delicate lists of modules that you have to constantly maintain for their build system, cabal).

Syntax inside the module:

module Pkg.Foo(bar) where

bar :: ()
bar = ()

Importing from within or from, er… not within a package:

import qualified Pkg.Foo as Foo

(I just noticed how unrelated the words “within” and “without” are, and I’ve spoken this language my entire life.)


#4

Thanks for help.

I’ve added a second table with how and when modules are compiled/added to the build.

I’ve noticed that Java-as-the-language may be different than Java-as-managed-by-IDE.