[net.lang.c] "Generic" Data

richw@ada-uts.UUCP (12/02/85)

Does anybody have any ideas for how one could implement "generic"
data types in C?  The term generic is borrowed from Ada;
"parameterized clusters" in CLU are similar.

For instance, I'd like to implement a "package" of routines which
deal with lists.  These lists can contain elements of any type
but share some operations which are independent of the type of the
elements.

A simple way to do this involves type casting, but loses because
type-checking is sacrificed.  Specifically, the following illustrates
the basic idea:

------------------------- list.h --------------------------
typedef struct List {
    char *car;
    struct List *cdr;
} List;

/**  I apologize for using the all-too-common Lispish terminology  **/
/**  "What do you mean, you don't know what `cdr' means?"          **/
/**  "Isn't it perfectly obvious???"                               **/

     :
extern List *cons();
     :
-----------------------------------------------------------
------------------------- list.c --------------------------
#include "list.h"

List *cons(car, cdr)
char *car;
List *cdr;
{
    List *result;

    result = (List *) malloc(sizeof(List));
    result->car = car;
    result->cdr = cdr;
    return result;
}
-----------------------------------------------------------

Users of such a package would need to caste the arguments and results
of these functions to and from (char *), e.g.:

#include "list.h"

          :
{
    List *int_list;
    List *char_list;

    int_list = cons((char *) 1, cons((char *) 2, NULL));
    char_list = cons((char *) 'a', NULL);
          :
}

Not only does the user play games with the type-checking, there also
is the problem of data-size -- machines whose char pointers are
shorter than ints or chars themselves (unlikely, I know, but...)
would die given the above.

Other ideas which use (ab-use?) the C preprocessor are possible,
but are much more involved.

-- Rich Wagner

dick@tjalk.UUCP (Dick Grune) (12/10/85)

In article <10200027@ada-uts.UUCP> richw@ada-uts.UUCP writes:
>
>Does anybody have any ideas for how one could implement "generic"
>data types in C?  The term generic is borrowed from Ada;
>  ..........
>Other ideas which use (ab-use?) the C preprocessor are possible,
>but are much more involved.
>
>-- Rich Wagner

Yes, you can do a lot of things that way, and, if you're willing to put
up with Ada's long-windedness, you can even make it look nice.  After
all, instantiating a package with given parameters in Ada is like
compiling in C with a given set of #defines in an #include file.

I've been experimenting with such trickery, just because I wanted
types and functions (real functions, not some int (*signal())() or so)
in C, and no duplicate code, and have it look nice to boot.

One important trick turns out to be to also instantiate the *.h file to
be used with the instantiated package.  (Why is it that sentences about
Ada are twice as complicated as normal language? Why is it that articles
about Ada are twice ... etc.?)

Also, make is quite cooperative in playing the APSE role.

I am presently summing it all up in a small article for SIGPLAN Notices
which I hope to finish between Christmas and New Year's Day (all scientific
work is done when honest people sleep or eat turkey).  If you are
interested, I can send you a sample program in this technique, but it's
too long to just post it (over 200 lines, and that's minimum.  Why is it
that anything concerning Ada is twice ...?)

					Dick Grune
					Vrije Universiteit
					de Boelelaan 1081
					1081 HV  Amsterdam
					the Netherlands