next up previous index
Next: Utility Libraries Up: Libraries Previous: Syntax differences

Structures with Field Names

   

This package allows the use of structures with field names. It is intended to make programs more readable and easier to modify, without compromising efficiency (it is implemented using ECLiPSe macros). The library is loaded using

:- lib(structures).
A structure is declared by specifying a template like:  
:- define_struct( book(author, title, year, publisher) ).
Structures with the functor book/4 can then be written as  
book with []
book with title:'tom sawyer'
book with [title:'tom sawyer', year:1886, author:twain]
which translate to the corresponding forms
book(_, _, _, _)
book(_, 'tom sawyer', _, _)
book(twain, 'tom sawyer', 1886, _)
This transformation is done by macro expansion, therefore it can be used in any context and is as efficient as using the structures directly. The argument index of a field in a structure can be obtained using a term of the form  
FieldName of StructName
E.g. to access (ie. unify) a single argument of a structure, use arg/3 like this:
arg(year of book, B, Y)
which is translated into
arg(3, B, Y)
For printing structures as with/2 terms, use portray_struct/2:
portray_struct(output, B)
which will print something like
book with [author : twain, title : tom sawyer,
year : 1886, publisher : _g117]
Remove the structure declaration using
erase_struct(book).


Micha Meier
Mon Mar 4 12:11:45 MET 1996