lua-users home
lua-l archive

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


On Wednesday 10 August 2005 18:52, Rici Lake wrote:
[...]
> C++, of course, provides the semantically odd reference type. Perhaps
> that's what you were thinking of:
[...]
> int& i = &ints->val;
> int& j = i;  /* ILLEGAL */
> int& j = &i; /* Tell the compiler *not* to dereference i */

Actually, you're not quite right there --- lines 1 and 3 are invalid, line 2 
is valid. (I think that makes you exactly wrong!)

A reference can be initialised exactly once from a variable of the same type:

int& i = j; /* i becomes an alias for j */

It can then be assigned to, but the assignment is actually performed on the 
target of the reference:

i = k; /* j's value is modified to be that of k's value */

You can't change the target of a reference once it's been initialised. (And 
very annoying it is, too.)

A better C++ analogy is as follows:

  class Proxy {
    int* target;

    Proxy& operator = (int value) { *target = value; return *this; };
    int operator int () { return *target; };
  }

  int fnord;
  Proxy fnordptr;
  fnordptr.target = &fnord;
  fnordptr = 42; /* sets fnord's value */

This is phenomenally useful if you want to, say, add locks around accesses to 
something, particularly when combined with generics. Take a look at C++'s 
smart pointers some time. This allows you to do things like:

  int value;
  locked<int> value_l = &value; /* initialisation */

  value_l = 1; /* lock, assign, unlock */
  printf("%d\n", value_l); /* lock, copy, unlock, return copy */

Very neat.

(Why, yes, I *have* been spending far too much time recently grovelling around 
in C++'s inner workings. I have grown to dislike it immensely, too. While it 
has some nice features, they're implemented so badly as to make them largely 
useless.)

[...]
>    aList->setval(aList->getval() + 1);
>
> rather than the arguably more readable:
>
>    ++aList->val;

You're not going to like me for this, but this is possible! It's a pain in the 
arse, though, and is usually not worth the effort. The key is to make val a 
smart object which has operator++ overloaded.

[...]
> It depends
> on defining a 'mutate box' primitive, which might be written ':=' or
> '<-'; the semantics of 'foo <- val' would be fairly similar to the C
> expression '*foo = val'.

*nods, although I'd write that last 'foo->assign(val)'*

C++ gets away with this because it has two types of thing; scalars and 
objects. (And references, but they're not relevant.) The = operator is 
'assign' for scalars but 'mutate' for objects. You can overload objects but 
not scalars. So this:

  Object foo;
  foo = 0;

...is a fundamentally *different* operation to this:

  Object* foo;
  foo = 0;

...despite syntactic similarities.

In Lua, everything is an object (or pretends to be one), but like Smalltalk 
and Java it has no 'mutate' operator. While one would be very useful, I do 
find myself about dubious about whether it's possible to implement it and 
still be able to call the result Lua!

> > http://www.thecommune.org.uk/~lisa/

Did you use to call yourself 'Communa'? Because if so, I think we've met...

-- 
"Curses! Foiled by the chilled dairy treats of righteousness!" --- Earthworm 
Jim (evil)

Attachment: pgpd9Z1q9gwll.pgp
Description: PGP signature