Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!crackers!jjmhome!smds!sw From: sw@smds.UUCP (Stephen E. Witham) Newsgroups: comp.lang.misc Subject: Re: On whether C has first-class composable functions Summary: Dynamically allocating functionoids Message-ID: <307@smds.UUCP> Date: 28 Jan 91 19:38:40 GMT References: <4408:Jan421:44:3391@kramden.acf.nyu.edu> <1991Jan26.132913.11358@spool.cs.wisc.edu> Organization: SMDS Inc., Concord, MA Lines: 40 In article <1991Jan26.132913.11358@spool.cs.wisc.edu>, quale@picard.cs.wisc.edu (Douglas E. Quale) writes: > > We'll forget polymorphism. With C it's hopeless. (Hence C++). Yeah. The impossible part is composing functions that RETURN different types. You could make all your functions deal with some "universal" type, like a pointer to a void, which you would cast to a pointer to a structure full of whatever, but that's pretty awful. By the way, C++ is sometimes implemented as a translator-into-C, so if you want to do something C++ does, but in C, ALL YOU HAVE TO DO :-} is make sure you're as thorough and methodical as a C++ compiler, and put up with some horrible syntax! > Show me an implementation that doesn't have a fixed limit on the number of > functions that can be composed. The only portable C implementation > demonstrated so far has an arbitrary limit compiled in. How do I > dynamically allocate function objects in C?? I guess you missed my earlier posting. Since it dealt with functions from ints to ints (and would require different compose functions to deal with functions that returned different types), I guess you don't want the details. The main idea, though, is that you invent a structure... struct Func { int (*f)( void *, int ); void *instanceVariables; }; ...and an "apply" function (it's dangerous as a macro): int apply ( f, arg ) struct Func *f; int arg; { return (f)->f ( (f)->instanceVariables, arg ); } The struct Funcs can be alloc'ed as you like. Mail me if you want the details. Or, if anybody wants, I'll repost it. --Steve Witham Not-the-fault-of: SMDS, Inc., Concord, MA