Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there programming languages with no global variables?

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):

>>> x = 3
>>> def fact(n):
...     print x
...     return reduce(lambda u, v: u*v, xrange(1, n+1), 1)
...     

would give an error because x is not defined inside the function fact?

In general, are there languages where the scope of any function wouldn't include functions defined within it?

Edit: 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 argument against OOP:

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.

Also relevant is that I hear that the language Newspeak doesn't has no global namespace, though I have no idea how it works.

I can imagine the issue, raised in Brian's comment below, of built-in functions (functions imported from __builtins__ 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.

like image 734
Muhammad Alkarouri Avatar asked Nov 21 '10 21:11

Muhammad Alkarouri


1 Answers

I'll try to roughly outline how it works in Newspeak.

Any code you write has to be in a module. Module is a kind of class, since in Newspeak classes can contain other classes, a Module is essentially a top-level class - one which is not contained in another class. What is special about Newspeak is that you cannot refer to anything outside of your module.

So how do you print to console in Newspeak? Printing belongs to Console class (a.k.a. Smalltalk's Transcript) which is part of the Platform module. To be able to print to console, your module would take a Platform constructor parameter, get console from the platform, store the console in a slot, and then use it to print.

Basically it's like dependency injection enforced on the language level. The language IDE and runtime help you package and bootstrap your program, but if you are looking for more details - go to Gilad Bracha's blog, see this post for example, or check out Newspeak Modules paper.

P.S. Newspeak is neither impractical nor unusable, for the record - it was used in the industrial environment, and now has a small (but growing) open-source community around it. Newspeak is very new and evolving, sure, but from personal experience - it is quite easy and fun to write programs in.

like image 83
Yardena Avatar answered Dec 03 '22 16:12

Yardena