cr88192
6/24/2011 9:54:00 AM
On 6/24/2011 1:42 AM, jacob navia wrote:
> The lcc-win compiler is an experimental compiler to promote the
> development of C as a language. Contrary to the main trends of language
> design this days, lcc-win considers C not a dead language but a language
> that can evolve and propose new features.
>
> In this context, one of the extensions that lcc-win supports is
> references, where the design is largely inspired from the C++ design.
>
a partial risk though is that C + bunches-of-extensions is no longer C,
rather, another C derived language (in a similar manner to C++ or
Objective-C, which are also not strictly C).
>
> What are references?
>
> In fact, a reference is just a pointer. It is different from plain
> pointers in two ways:
>
> (1) It is always assigned to a concrete object when it is defined.
> (2) Once defined, it will always point to that same object. It can't
> be changed to point into another object.
> (3) It is used with the '.' notation, as if it weren't a pointer.
> (4) Instead of "*' the symbol "&" is used:
> int i = 23;
> int &IntReference = i;
>
> As always in this proposals, the problems hide in the details. For
> instance, several days ago someone complained that a reference to
> void would not compile, but a pointer to void would. This prompted
> me to correct a problem in my implementation: I should have forbidden to
> build references to void!
>
> The problem is when defining a reference but when you pass a reference
> to a function:
> int fn(void &ref);
> In this case there is no initialization (that would explode immediately)
> but just a definition without any initialization. The compiler must
> check, then, that the reference is not to a void pointer.
>
> Since the object pointed to by a reference can't change, many people
> think that the value can't never change but that is wrong. Consider:
>
> #include <stdio.h>
> int main(void)
> {
> int i = 23;
> int &pi = i;
>
> i = 3;
> printf("%d\n",pi);
> }
>
> The reference is initialized to an integer whose value is 23.
> If the integer changes its value, the reference (like all other
> pointers) changes its value also.
>
> The advantages of using references are that they are never NULL,
> and that they always point to the same object, two important
> things less to test. The only (orthodox) way of declaring a non-NULL
> pointer in an argument list is now:
>
> int fn(int arg[static 1]);
>
> This declares that the "arg" argument will have at least one integer
> Few people know this, and it wasn't much explained when C99 introduced
> it.
>
going OT:
interestingly, in my own current language (more closely related to
JavaScript and ActionScript), I ended up giving references a "look and
feel" more like that of C pointers.
basically, the caller needs to use "&" to pass an argument to a
reference, and by default (no '&' in the argument list) it would be
needed to use '*' to access/assign them. if '&' is given, it means to
insert an implicit '*' on access, and later could also insert basic
sanity checks (barf if the argument is not "sane" when calling the
function).
as-is, I figured doing calls like "foo(&x);" to be a reasonable
trade-off (it serves a similar role to 'ref' in C#).
note that, unlike C, these are not raw pointers, rather a
boxed-pointer-type (some languages use the term "locative" for these
instead of "pointer" or "reference").