[comp.lang.icon] Records

R.J.Hare@edinburgh.ac.uk (05/28/91)

I'm just about to start writing my first program which uses records.
Question, can I write a record straight to a file without separating the
fields - like this:
 
record line(a,b,c,d)
procedure(main)
  .
  .
output_line:=line(w,x,y,z)
write(file,output_line)
  .
  .
 
or do I have to do it a field at a time:
 
  .
every i := 1 to 4 do write(file,output_line[i])
  .
 
Any advice appreciated.
 
Thanks.
 
Roger Hare.

R.J.Hare@edinburgh.ac.uk (05/31/91)

Thanks to all those who answered my query about records.
 
Next question - I am using records as the basis for a simple database
program. Given that the program will be written in a tight enough way to
ensure that it won't be possible to write a list with more elements than
there are in a database entry, what is the advantage of using a record
to store a database entry as opposed to a list?
 
I've got my own ideas on this, but I'd like to canvass a few other
opinions.
 
 
Thanks.
 
Roger Hare.

cargo@CHERRY.CRAY.COM (David S. Cargo) (05/31/91)

I use records instead of lists in cases where I want a more
mnemonic way of accessing structures.  In a program I'm working
on right now, I can tell what structures are compatible because
I have named the fields the same names in the same order.  It
would not be possible to decide that so easily if the lists were
simply the same size.

Likewise, the fact that records are their own types allows me to
base actions based on the type of a particular parameter.  I have
exploited these factors together in the following fragment of code.
Because the name of the record type is also the name of the record
constructor for that type, the following routine returns a value
whose type is the same as the type passed to it without knowing
what that type is.  As long as the type is a record with at least
one field named entries, this fragment will work.

## entry_name -- return the entry portion of an absolute path
procedure entry_name(a_path)
#   Uses globals
#       verified   (record constructor)
#       unverified (record constructor)
#   record verified(entries)
#   record unverified(entries)
    if *a_path.entries > 1
    then return type(a_path)(a_path.entries[-1])
end

A lot of the code I write takes advantage of the type information
carried along by record values.

David S. Cargo (cargo@cray.com)

goer@ellis.uchicago.edu (Richard L. Goerwitz) (05/31/91)

In <31.May.91..11:23:12.bst..060024@EMAS-A> R.J.Hare@edinburgh.ac.uk writes:
> 
>Next question - I am using records as the basis for a simple database
>program. Given that the program will be written in a tight enough way to
>ensure that it won't be possible to write a list with more elements than
>there are in a database entry, what is the advantage of using a record
>to store a database entry as opposed to a list?

Record field access is much faster than list subscripting.  You can also
get at the fields by name, and not worry about their order.  If you want,
though, you can subscript records (as long as you realize that doing so
erases most of their method of/speed of access advantages).

If you really want to be clever, use a record for all the fields you know
you'll be using, but make one member of the record into a list where you
can store optional or other fields.  You'll get the best of both worlds,
at the expense of some clarity and straightforwardness.

-- 

   -Richard L. Goerwitz              goer%sophist@uchicago.bitnet
   goer@sophist.uchicago.edu         rutgers!oddjob!gide!sophist!goer