3 ms·
We have such a theory, it's called "modules". But unfortunately, modules are even less popular than proper typesystems have been for decades. It's no coinciden
by choeger 4y ago
We have such a theory, it's called "modules". But unfortunately, modules are even less popular than proper typesystems have been for decades.
It's no coincidence that Rust (which has basically copied Haskell's type system to a large degree) and Julia (which is essentially a Lisp in disguise) have somewhat sound packaging systems. C/C++ does not even have a tidy core language, let alone proper modules (yes, yes, C++ modules might change that, but they are still a complex mix of templates and normal code).
- pastage 4y agoI do not understand what you mean by modules, and how Rust and not Java or Python is helped by this.
- simiones 4y agoModules typically refers to a unit of code that packages together several types and functions (and/or other top-level concepts in your language, e.g. macros, templates, metaclasses, namespaces, packages) with an explicitly declared public interface, while still allowing these types and functions to have additional internal APIs that are not exposed. A module has to declare what other modules it depends on explicitly. The public interface of a module has to be explicit enough that a compiler for that language only needs the public interface description of that module to compile code that depends on it. A good example of the most widely used and successful module system is C compiled libraries. The header files represent the public description of a library (or, for dynamic linking, the .so/.dll itself embeds this info as well), and a C compiler only requires these header files to be able to compile dependent code and later link the two. It is also impossible to access non-public members of the library from standard C (without resorting to direct memory access, of course). Java packages don't have these properties because of two problems: there is no way to indicate dependencies between packages, and there is no standard way to have a bundle that corresponds to a package (since different .jar files can all load classes into the same package). The Java module system was created specifically to address these problems (and is still not fully adopted throughout the ecosystem). C++ breaks C's module system because of its heavy reliance on templates, which have to be entirely declared in the public API and can't have any internal API of their own; and because the compiler needs to know the total size of a class, including the size of its private members, to compile any code which references objects of that class. For Python, I'm honestly not sure if Python packages fit this definition of a module or not.
- pjmlp 4y ago> C++ breaks C's module system because of its heavy reliance on templates, which have to be entirely declared in the public API and can't have any internal API of their own; and because the compiler needs to know the total size of a class, including the size of its private members, to compile any code which references objects of that class. Other languages with generics and modules do just fine by having the required metadata on the module, e.g. Eiffel, Modula-3, Ada, OCaml,..., which is what C++20 modules do as well. I have been using C++ modules on VC++ for quite some time, and while there are still some warts on the overall experience, they work quite alright.
- simiones 4y agoSure, I should have said "C++ before the modules feature". I was thinking more to explain why the modules feature was required at all (when it's not really required too much for C). Also, C# Assemblies solved this problem for generics too, for a more mainstream example.