lua-users home
lua-l archive

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


2015-07-08 19:38 GMT+02:00 Parke <parke.nexus@gmail.com>:
>> 2015-07-07 0:13 GMT+02:00 Coda Highland <chighland@gmail.com>:
>>> A large proportion of the tuple users I've met are Python converts.
>
>> 2015-07-08 2:34 GMT+02:00 Tim Hill <drtimhill@gmail.com>:
>>> You can think of them as an aggregate constant.
>
> On Wed, Jul 8, 2015 at 1:43 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>> The main reason why aggregate constants would be nice to have in
>> Lua is that more lurking bugs can be caught at compile time.
>
> How so?  Can you provide an example that would result in a compile time error?

Back to my first post:

> Tuples are a special case of constant values. E.g.
>
> const c = {1,2,3}
>
> would create a constant table with three elements and associate it
> with the name c. The interpreter will remember that _ENV.c for this
> _ENV may not be redefined. A constant value can only be assigned
> to such constant table fields.

In the meantime I have realized, thanks to some postings in this thread,
that 'c', above, should be a local variable, not _ENV.c. The overhead in
tracing individual table fields would affect every key, but local variables
are not table entries.

If now:

c[2] = 1.5

the interpreter would know that `c` is const and generate "attempt to
change a constant object".

Similarly:

a = c

would be generate "attempt to assign a constant object to
a non-constant name", but

const a = c

would be OK.

It's quite similar to what happens in C. Try the following:

#include <stdio.h>

main() {
  typedef struct {
    double x, y;
  } complex;
  const complex I={0,1};
  I.x = 10;
  printf("%g %g\n",I.x,I.y);
}