Note that there are some explanatory texts on larger screens.

plurals
  1. POAre there programming languages with no global variables?
    text
    copied!<p>Are there languages where the scope is defined in such a way that does not extend to the enclosed functions? In other words is there a language where a code like the following (Python-like syntax):</p> <pre><code>&gt;&gt;&gt; x = 3 &gt;&gt;&gt; def fact(n): ... print x ... return reduce(lambda u, v: u*v, xrange(1, n+1), 1) ... </code></pre> <p>would give an error because x is not defined inside the function <code>fact</code>?</p> <p>In general, are there languages where the scope of <em>any</em> function wouldn't include functions defined within it?</p> <p><strong>Edit</strong>: Thanks for the informative comments. The reason I thought about this is that the situation of an internal function having access to all the environment provided by its containing functions sounds suspiciously close to me to the situation described by Joe Armstrong in his <a href="http://www.cloudknow.com/2009/08/review-coders-at-work-by-peter-seibel/">argument against OOP</a>:</p> <blockquote> <p>Because the problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.</p> </blockquote> <p>Also relevant is that I hear that the language <a href="http://en.wikipedia.org/wiki/Newspeak_%28programming_language%29">Newspeak</a> doesn't has no global namespace, though I have no idea how it works.</p> <p>I can imagine the issue, raised in Brian's comment below, of built-in functions (functions imported from <code>__builtins__</code> in Pythonspeak or System in many other languages) be introduced artificially by the interpreter/compiler in every function. After all they are almost always treated specially in the language in the first place. Another option is to have them as methods of an object passed as a parameter to the function or imported as a module from within.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload