Reputation: 1786
When writing Smalltalk code that uses temporaries you write something like:
SequenceableCollection>>#swap: index1 with: index2
| temp |
temp := self at: index1.
self at: index1 put: (self at: index2).
self at: index2 put: temp.
I find the syntax for declaring temporaries a bit old fashioned and cumbersome. Actually, is one of the places where you usually stop thinking in your domain and focus in computer (you wrote your method, you are ready to accept the code, but have to "cleanup" your temporaries zone). It is distracting.
Maybe temp declarations are useful for performance? or even declarativeness -which is anyway improved by IDE tuning like syntax highlighting, but I think the | temp |
could and should be optional. The compiler has all the needed information, or could make the needed assumptions if the user didn't provide a temp declaration (assume the temp lives in the closer environment).
What would be the problems of implementing such a change?
Upvotes: 4
Views: 139
Reputation: 3200
There are no technical problems - it would be trivial to change the compiler, technically.
But it would be a maintenance and debugging nightmare, as already pointed out by others: what happens if a same-named variable is later added in an outer scope (and that might be an instvar way up in the inheritance hierarchy)? Also, what if you simply have a typo, and a temporary was not your intention, but you actually wanted to access some existing variable?
Having programmed for a long time (and in languages which do automatic variable definition), I would not want this. It might be ok for little 10-liner scripts, but not for big programs which have to be maintained over a long time. As Bert pointed out, workspaces usually do this for you and that is fine, but not in methods which go into production code.
I remember an old story about a fortran program which crashed, because the programmer wrote "do10i=10.20" instead of "do10i=10,20" and got a new variable named "do10i" assigned to 10.20 instead of a do-loop. He would have been warned, if he had to declare variables first (admitted: fortran was an ugly language then, but you get my point, I guess).
Upvotes: 0
Reputation: 4958
Would it be possible to make declaration of temp vars optional in Smalltalk?
Sure - the compiler would still know which variables are which at compile time, i.e. when you save the method.
What would be the problems of implementing such a change?
The main problem is that syntax highlighting doesn't cover all cases where things might get... ambiguous:
TL;DR: Finding something "old-fashioned", "cumbersome", or "obtrusive" (as much as I may agree with you) does not trump clarity and disambiguation.
Having to declare temporaries comes with the territory, so to speak. Additionally, I don't think a quick "cleanup" of your method before you accept it is a bad thing.
Upvotes: 2
Reputation: 14868
This is a dialect dependent feature. In some dialects if you don't declare the temporaries the browser will do it for you when saving the method. Personally I find this behavior more convenient than the one where the browser warns you every time you use an unknown identifier because, as you say, that can be distractive. By deferring the definition of temporaries to the time when you save the method you don't have to "shift" your thinking from the domain; the browser will complete the details. The other advantage of not declaring temporaries "by hand" is that your methods won't end up declaring unused temps.
Upvotes: 2