3 ms·
> Ruby's require is analogous to C's #include: modules are just files that get evaluated in order to add stuff to a global interpreter state. What's the theore
by thaumasiotes 3y ago
> Ruby's require is analogous to C's #include: modules are just files that get evaluated in order to add stuff to a global interpreter state.
What's the theoretical alternative? Adding stuff to a global interpreter state is the goal.
The weird part of #include, to me, is not that it ends up modifying the global interpreter state. That's the point of calling it! The weird part, what C libraries tie themselves into endless knots to work around, is that when you #include something in more than one place, it will execute more than once.
- riffraff 3y agoThere's at least a few? E.g. * Lexical scoped imports a-la python * Pass state to modules explicitly, which I think is what newspeak was doing (i.e there's no global state) Either way you'd lose ruby's "let's add methods to Kernel and have them pop up everywhere" so it's a trade off. Worth nothing: there are a few proposals around to have some sort of namespaces in Ruby, but it's unclear they'll ever happen. E.g. https://bugs.ruby-lang.org/issues/19744 https://bugs.ruby-lang.org/issues/19744
- thaumasiotes 3y ago> Lexical scoped imports a-la python How is this an alternative to modifying the global interpreter state? Before I do any imports, I can type this: os.getcwd() and the interpreter will crash, telling me that "os" doesn't mean anything. But if I do this: import os then, later, I can type exactly the same thing: os.getcwd() and the interpreter will call the function and return a string. In order for the interpreter's behavior to be different when presented with exactly the same input, it's necessary that the state of the interpreter has changed. What's the alternative? And since this is the entire purpose of a module system -- causing the global interpreter to recognize names that it otherwise wouldn't have -- it follows immediately that all module systems will operate by making changes to a global interpreter state. The reason Python's module system is better than C's has nothing to do with whether or not either system modifies a global interpreter state, since that doesn't differ between the systems. The reason is that Python allows you to specify what you want those modifications to be. If I have two Python libraries named os, I can import them like so: from lib1 import os from lib2 import os as os2 and now they don't conflict. As far as I know, C has no such facility.
- mirekrusin 3y agoIf you qualify imports transitive dependencies won’t pollute your scope. It also naturally deals better with name conflicts, allows cherry picking exports etc.
- thaumasiotes 3y agoOK, where's the part of this where we're not modifying a global interpreter state?
- tomstuart 3y agoYou’re taking the use of the word “state” too literally. The original comment was complaining about how Ruby `require` adds names (constants) to a global namespace; Python `import` only binds names in the local scope.
- jeltz 3y agoBy that definition I would not say any language has modules. Even compiled ones
- movpasd 3y agoI expect _global_ here refers to the language-level semantics, not internal interpreter semantics. Python's module system results in no implicit modifications to Python-level state between modules — at least, for a well-developed module. Since Python is so dynamic, you can of course jerry-rig all sorts of global consequences if you really want to. But without dynamic magic, all state is namespaced to the imported module, and must be explicitly given names within the importer, with no global consequences for parallel modules. In your os.getcwd() example, the state is scoped to the importer module, so it's not an example of global interpreter state. I don't know what Ruby's module system looks like, so I don't know for sure if that is the point the GP is making. It seems to me that you are in agreement, just disagree with the meaning of "global" in this context.
- thaumasiotes 3y ago
- boxed 3y agoWaitwhat, surely it's not GLOBAL state, but "module global" or something?
- zbentley 3y agoIn Ruby, it really is global. Imports can (and do) modify the behavior of the stdlib, and any other libraries' namespaces in the same way that they would manipulate their "own" code and data.
- boxed 3y agoI mean, sure, the monkey patching/extending is a thing. But if you import "foo" in one ruby file, surely it doesn't pollute the namespace of all other ruby files loaded after with that symbol in their global scope?
- trashburger 3y ago> What's the theoretical alternative? Adding stuff to a global interpreter state is the goal. This is the right way to do it but it suffers from a lack of correct tooling. The Self[0] programming environment assigns a "module" to each "slot" (class methods in Ruby land); an object may be composed from multiple modules at once. If Ruby had this kind of granularity in its inclusion system I believe that a lot of the confusion around where a particular method comes from would disappear. [0]: https://selflanguage.org/ https://selflanguage.org/
- matheusmoreira 3y agoThe alternative is isolated namespaces. Every module maintains an independent symbol table.
- thaumasiotes 3y agoOK, so now we load the module, it maintains its own independent symbol table, and we have no way to access that table. Our code is unable to use the module, but at least we can content ourselves that it's loaded. (On the other hand, why bother giving it a symbol table at all?) What if I want to write some code that will use the module I've loaded?
- matheusmoreira 3y ago> and we have no way to access that table Correct. We must create such mechanisms. The aptly named import mechanism seems to be the most common solution to that problem. There's a heap with objects floating freely around, and two tables of symbols and the objects they point to. The sets of symbols in each table may intersect freely: both tables might have an "X" symbol without clashing with one another. On the other hand, there's no intersection between the sets of objects they point to at first. They start out fully isolated and their values are unreachable from the outside. As you noted, this is of limited usefulness to programmers. There's no point in collecting symbols into a module if the values those symbols point to are unreachable. We need access to foreign symbols in order to make use of abstraction. To that end, the implementation may provide a symbol importing mechanism. It could be as simple as setting a symbol on the current table to the object pointed to by a symbol of the other table. In fact that's exactly how I implemented it in my language. So this "imports" the symbol from another table to the current one. Both tables were completely isolated at first but now there are explicit links from one table to the other: there's two symbols from two separate modules pointing to the same object in memory, linking one module to the other. That object is most likely a function and importing it allows the programmer to call it. > On the other hand, why bother giving it a symbol table at all? Because symbol tables are how variables are implemented. They're literal hash tables mapping symbols to arbitrary values, symbols are just unique strings. If you don't give modules a symbol table, you are unable to set variables at the module level.