[Nickle] Reference type modifiers

Keith Packard nickle@nickle.org
Sun, 16 Mar 2003 22:41:01 -0800


I've added 'reference' types to nickle.  These are functionally identical 
to pointer types except that values of this type implicitly refer to the 
referent rather than to the refererence.  To refer to the reference it
self, prefix the expression with an ampersand (&).  

In other words, you use a value of reference type in the same situations
you would use a value of pointer type except that everywhere you would 
have typed '*p', you type 'r' and everywhere you would have typed 'p', you 
type '&r':

	> void foo (&int j) { j = 7; }
	> void bar (*int k) { *k = 7; }
	> int l;
	> foo (&l);
	> l
	7

These two functions are functionally identical; they accept the same 
values and perform the same function.

	> int i = 12;
	> int j = 100;
	> &int r = &i;			r is a 'reference to int'
	> r				r with no modifiers is the refernt
	12			
	> *int p = &j;			p is a 'pointer to int'
	> p				p with no modifiers is the reference
	&100
	> &r = p			&r is the reference
	&100
	> r = 10			store into r changes the referent
	10
	> *p				*p is the referent
	10

The hope for reference types is to make passing values by reference less
clumsy looking:

    /*
     * Quicksort with random pivot
     */
    public void function qsort (&poly[*] a, bool(poly, poly) gt)
    {
        void function quicksort (int p, int r)
        {
            if (p < r)
            {
                /* swap two array elements */
                void function exchange (int i, int j)
                {
                    poly t = a[i]; a[i] = a[j]; a[j] = t;
                }

                /* partition the array into two pieces and return the pivot */
                int function partition (int p, int r)
                {
                    /* select a random element to pivot */
                    int pivot = p + PRNG::randint(p-r);
                    exchange (pivot, r);
   
                    poly x = a[r];
                    int i = p;
                    for (int j = p; j < r; j++)
                    {
                        if (gt (x, a[j]))
                        {
                            exchange (i, j);
                            i++;
                        }
                    }
                    exchange (i, r);
                    return i;
                }
                
                int q = partition (p, r);
                quicksort (p, q-1);
                quicksort (q+1, r);
            }
        }
    
        quicksort (0, dim(a)-1);
    }

Without reference types, this code would be full of the (*a)[n] 
constructions, or worse the horrid *[ operator invented just for this 
purpose.

The intent is for this to look like reference types in ML or var 
parameters in Pascal.  Note that unlike C++, you don't implicitly get 
references constructed for arguments; this should avoid fun surprises that 
often crop up in C++ programs, but it does mean that you'll need to add & 
in front of arguments to reference-formaled functions.

-keith