lua-users home
lua-l archive

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


> I am not using variables for lua - host communication, but it would be a
lot
> more easy if I could
> do parts of the configuration simply setting a variable. Imagine: one of
the
> tasks of lua scripts is
> to set the account as a debit or a credit. One function call to say SetDC
> ( "Debit" ) ?.

Perhaps:

  account.type = "Debit"

Actually it would be a particular account, no? So you would say:

  Account(10030).type = "Debit"

for example.

Or perhaps:
  ManchersterOfficeRent = Account{code = 10030, type = "debit", ...}

Or:

MOR = Account {
  code = 10030,
  description = "Manchester Office Rent",
  type = "debit"
}

Plus other possibilities. I think the Lua syntax is very flexible in this
regard.

But that was my point about assignment sending a message to the container,
not to the value. If you used the first one (account.type = "debit"), it is
the object "account" which is being told to set its type to debit. To me,
that makes sense, and it makes just as much sense in the global
environment,

For example:

TimeZone = "gmt"

is a reasonable way of telling the global environment what the time zone
is.
It was precisely this line of thinking that promoted the code that I put on
the Wiki.

I think that it is not very different from what you want, really. It is
only
how you think about it. Once you think of assignment as sending a message
to
the container (whether it is a table, a userdata, or the global
environment),
then you no longer think about assignment as a value method.

So in the above example, one line of thinking would say that I set TimeZone
to an object (a userdata, say), and then assignment magically changes to
being
an object method.

But the other way of thinking is that I tell the global environment that
TimeZone is a special variable, and then assignment stays being a message
to
the global environment.

Now, the only question is how I tell the global environment to make
TimeZone
special. I could do that with assignment, of course, since that is a
message
to the global environment. Or I could use some function to register it as
a special. Either way, I am not doing much differently from what you are
asking for, I am just thinking about it differently (and, I would argue, in
a way that is more consistent with functional languages such as Lua.)

> Of course, without a binding, to query what the script does to lua
globals
> is worser than a function call. That is my point.

In the sense that you have to query rather than being told, yes.

> Also, I know that assignment in lua is not exactly like other languages.
An
> object has no value by itself. But that may be ok for the native lua
> objects. For more elaborated ( userdata ) this comes to cut the whole
idea !

See above.

> Also, the question of binary operators: they must be the same type. So
> c = udata( 4 )
> d = 6
> if c < d  -- assertion failure
> then

> what if my metamethod __lt can cope with any type ?

I take your point here, but it is a contentious issue. OO languages do
not deal very well with binary operators, and there is much debate about
this issue. What if c and d both have _lt metamethods? Which one wins?
Why should it be the first one (if that is what you are thinking)?

Binary dispatch is probably the answer, but that is complicated (and
can be slow, too.)