[comp.lang.c] More "D"

pardo@june.cs.washington.edu (David Keppel) (03/06/88)

[ Anonymous structures.  Like to be able to return them from functions ]

If you want to return structures from functions, don't use anonymous
ones, since the caller as well as the callee (and me, when I read the code)
must know the type.

>>> What follows is ideas that don't have to do with current "C" <<<

It might be more efficient for you to be able to return structure values
without having to declare a temporary:

    struct foo_s {
	int	a, b;
    }

	struct foo_s
    bork()
    {
	register int	i, j;

	do {
	    /* i & j used a whole bunch of times */
	} while (expr);

	return( foo_s.a = i, foo_s.b = j );
    }

If the compiler wants to hand in a pointer to the structure to return,
then the values can be saved there directly, rather than saving them to
some intermediate temporary (which won't fit into a structure ;-) then
copying that to the return space.

As an option to the munged syntax above (struct_name.field = val), there
could be a return "meta-variable" called "self" (or some such) to
return into.  It has the same type as the return type of the function:

    return( return.a = i, return.b = j );	/* no new keywords, anyway! */

This mechanism doesn't say anything about the efficiency of accessing the
return "variable" versus declaring a temp and using that in the body,
since in general the meta-value would require at least pointer indirection.
Similarly, the meta-variable would probably need to be "register" class,
since (Uhh.. I think) we don't know in general that we can take the address
of it to pass it to other functions:

    bletch( &return );

and this *certainly* exposes aliasing problems if we do!  And how about:

    return.func = function_name;
    return( return.val = (*return.func)(&return) );

for the grossest overloaded sintax you've ever seen!  (Don't flame me,
I'm trying to convince you *not* to do this!)

I nominate the simple version of this for D.  There's no reason why it
couldn't be in C, and if somebody wants to add it to GNU cc, I'll use it!

	;-D on  (register void main())  Pardo