lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Soni L. once stated: 
> >On Sun, Jun 7, 2015 at 1:38 PM, Soni L. <fakedme@gmail.com> wrote:
> >>
> >>Here's better syntactic sugar:
> >>
> >>v->abs = v:abs()
> >>
> >>(in C, -> is like . but with a dereference, "dereference" being the key
> >>here)
> >>
> >Why not
> >    v\abs!
> >?
> >
> >It is the moonscript syntax.
>
> But -> is something a C coder is used to.

  And something some C coders (like me) hate.  It doesn't need to exist at
all.  Take, for instance:

	struct foo
	{
	  int a;
	  int b;
	  int c;
	} foo;
	int bar[3];

  foo and bar will have the same layout in memory (and be the same size).
And to reference the second int:

	int a = foo.b;
	int b = bar[1];

  The difference in syntax here is because the two are conceptually
different.  foo is a structure and the fields can be of different types. 
bar is an array, where each element is of the same type.  Now, let's mix
things up a bit with pointers:

	foo *pf = &foo;
	int *pb = bar;

  Right off the bat, you'll notice something a bit different.  Why isn't the
second line:

	int *pb = &bar; /* ? */

  But before I answer that, let's grab the second int through the pointers:

	int c = pf->b;
	int d = pb[1];

  Wait ... what?  If pointers were treated more consistently, then you would
expect either ...

	foo *pf = foo;	/* personally, I prefer this style */
	int *pb = bar;

	int c = pf.b;
	int d = pb[1];

or

	foo *pf = &foo;
	int *pb = &bar;

	int c = pf->b;
	int d = pb->[1];

  So, if C (or rather, the C compilers) can tell the difference between an
array and a pointer to an array and still use the same syntax, then it
should be just as easy (or the same) for C (or rather, the C compiler) to
tell the difference between a struct and a pointer to a struct and use the
same syntax.  The only reason we have what we have now is historical [1][2].

  -spc (Who would prefer that -> never had existed in C)

[1]	http://stackoverflow.com/questions/13366083/why-does-the-arrow-operator-in-c-exist/13366168#13366168

[2]	K&R could probably have fixed it, but they already had 10 users ... [3]

[3]	It's a joke.  Laugh, son!  Laugh! [4]

[4]	Okay, I'll explain.  It's a reference to make and why we are still
	forced to use tabs in makefiles.  By the time the author realized
	his mistake, he already had 10 users ...