Packages in programming languages
Introduction
With modular programming, concerns are separated such that modules perform logically discrete functions, interacting through well-defined interfaces.
The module / package concept is formally supported by wide range of programming languages but many of them support package / module concept in different ways.
Some encapsulates symbols around single file, like for eg. Rust, Python; other encapsulates symbols around wider scope like Java encapsulates modules around Java Package.
There are few key aspects around programming language support for module / package concept.
Identity
Some languages use package name while resolving used / imported symbols, some not. In Rust package name used by Cargo tool doesn’t mean anything while in Python and Java it is a part of symbol name used while resolving symbols.
Scope
There are programming languages differentiate meaning of module and package, in Python scope of module is a single file while in Java module adds a higher level of aggregation above packages - which are directory / namespace scoped units, in Rust single package is one or more crates that provide a set of functionality, crates are library or binary and consist of modules.
Compiler directives
Language like Rust can compile source code written in two
different editions
- more or less slightly different dialects through
edition
attribute.
Symbol autoloading
In Rust there is a
build
,
include
and exclude
package attribute, in Python there is __path__
attribute is used during imports of its subpackages.
Exported symbol list
Used in JavaScript through
export
statement is used when creating JavaScript modules to export functions, objects, or primitive values from the module
and Java through exports
module directive specifies one of the module’s packages
whose public
types (and their nested public
and protected
types) should be accessible to code in all other modules
in Python namespaced module
__all__
attribute is used as a construct which instructs loading machinery which symbols to load.
Required modules / packages
In Rust it is possible to specify
dependencies
using package attribute, other languages like Java has
a requires
and requires transitive
module directive.
Conclusion
Following above list of package / module key aspects might want to think of adopting similar concept in PHP.
At first what can benefit could potentially be:
- identity as a part of FQCN which allows identitying membership to explicit package;
- scope aggregating symbols with namespaces and in root package namespace;
- compiler directives this is the place where
declare
directives should exist; - symbol autoloading could potentially be a good start point in restructuring current autoload machinery adding support for other non-class symbols;
- exported symbol list this potentially is far future which could help providing package symbols visibility;
- required packages also far future, currently fulfilled by Composer Packages;