[comp.lang.icon] final clause

nowlin@iwtqg.att.com (11/28/90)

> Icon is a pretty nifty programming language. I like and use the 'initial'
> construct. But sometimes I wished I had a 'final' construct. This may be
> a crummy example, but it illustrates the concept of the final pass through
> a procedure. Perhaps this isn't even implementable? Any comments on it's
> usefulness or implementation problems?

I don't see anyway to implement a final clause.  You might have the
interpreter go through all the declared procedures when it's told to
terminate and execute the final clauses.  Even then how do you know that
the order of execution of final clauses isn't significant?  How would you
handle run time errors or external interrupts?

If that won't work, how could the interpreter possibly know that a
procedure won't be executed again?  I don't think procedures ever go out of
scope in Icon so that's not a valid criteria.  What happens when you
declare a variable with the same name as a procedure?  What happens when a
procedure is assigned to a variable?  I don't see any way to do this.
Is this an attempt to get construction and destruction type constructs for
procedures?

> procedure print(fo,line)
>   static ff, pageno, line_count
>   initial
>     {
>     ff := ""
>     pageno := 0
>     line_count := 0
>     }
>   if (line_count -:= 1) < 0 then
>     {
>     write(fo,ff,"title line ",(pageno +:= 1))
>     line_count := 55
>     ff := "\f"
>     }
>   write(fo,format(line))
>   final
>     {
>     output_summary()
>     }
>   end

The example above has some problems since the output_summary() procedure is
called as part of the final clause but it isn't passed the parameters that
were initialized in the initial clause.  I get the idea though.  This could
be handled by making the 'fo' and 'line' parameters optional.  If they're
missing invoke the equivalent of the final clause.  I use optional
parameters frequently although not usually for this kind of thing.

Jerry Nowlin
(...!att!iwtqg!nowlin)

djbailey@SKYLER.MAVD.HONEYWELL.COM ("BAILEY, DON") (11/28/90)

Has anyone else suggested the simple brute force solution to implementing
a "final" clause?  Just push your program down a level in the calling 
sequence.  For example,

procedure main(args)
#
my_real_program(args,final_parameter_list)
do_final_work(final_parameter_list)
end

The real program can have an initial clause if it really needs it.
Whenever it finishes, "do_final_work" will be called. At worst, you
have to make some extra data global and deal with the success or 
failure of your program.  This approach lets you invoke different
final processing, by adding an "if", depending on success or failure 
or a state variable you pass back up.

-- Don J. Bailey  (djbailey@skyler.mavd.honeywell.com)