[Date Prev] [Date Next] [Thread Prev] [Thread Next] Date Index Thread Index Search archive:
Date:Mon Sep 9 14:59:58 1994 
Subject:Re: Variable declarations 
From:Steve Knight 
Volume-ID:940909.01 

James writes about the infelicitous "vars x = EXPR" syntax:
> Would it be sensible to introduce the syntax 'vars y -> x'?
> We have talked about this one before, but this time I am paying attention!

I think that the construction would more naturally be
        y -> var x;
Running the assignment arrow in this direction is consistent with 
its use in the rest of the language.

Note the use of -var- rather than -vars- to indictate that
a single target variable is being introduced.  This is to
solve the ambiguity of the statement
        y -> vars x, z;
             ^^^^ plural

A less good alternative would be
        var x <- y;
My criticism of this form is that it admits the following construction
        var x <- x;
which gives rise to EITHER a undefined value whilst generating no error 
messages OR an exception to the left-to-right reading of Pop.  Of course,
this is also a criticism of the existing 
        vars x = y;
syntax.

Whilst on this subject, let me risk a further bit of basenote drift
by pointing out the merits of Pepper-style "val" declarations.  Instead
of "var" one uses the keyword "val".  A variable declared as a 
"val" can only be assigned to once but reference many times.  In other
words it is a WORM-variable -- exactly in the style of Pop constants.
Unlike constant declarations, however, "val" variables can be declared
as procedure locals.  Unlike lconstant declarations, they are executed
in the run-time environment.

Why do I mention them?  Because IF all input/output locals and for
loop variable were automatically declared (by default) as "val" then
another class of typographical errors would be eliminated at a stroke.
Admittedly, this would mean that some idioms would be slightly less
elegant.  For example, the following would be trapped ....

        define check( x ) -> x;
            unless x.is_fine do
                mishap( 'INSUFFICIENTLY FINE VALUE', [ ^x ] )
            endunless
        enddefine;

It would have to be written as

        define check( var x ) -> x;
            ... etc etc ...

which, in my view, is a very small penalty to pay for a significant
improvement in program readability and safety.  To illustrate the 
readability point, here's a common routine written in the Pepper-style.

        define sum( L ) -> var total;
            0 -> total;
            for i in L do 
                i + total -> total
            endfor
        enddefine;

Note that the header "sum( L ) -> var total" immediately sets your
expectations.  The variable "total" will be repeatedly assigned to
during the procedure-call.  So when one encounters the first assignment,
one does not readily fall into the trap of thinking that this is
the final result.  

Steve