|
2015-12-26 8:55 GMT+02:00 Abhijit Nandy <abhijit.nandy@gmail.com>:
>> I suspect that relaxing this rule would not make the parser much
>> slower; the simplicity of Lua is intended to help the _human_ parsing
>> the code. E.g. the famous C/C++ mistake 'if (a = b)' is not possible.
>> Code is easier to read when there aren't too many possible surprises.
>
>
> Lets says speed was not much of an issue, then what modifications would be
> needed to have assignment as an operator in tables.
>
> So if there is say, an __assign() meta-function present in a table, then
> that is called with the value being assigned.
>
> Also C++ allows references on the left hand side of an assignment statement.
> So a function which returns a reference can have a value assigned to the
> returned variable.
Lua has names and values. An assignment statement is nothing
else than the association of a name with a value. Lua does not
have references.
If you wish to understand Lua, forget everything you know about
every other language you have ever used. Particularly C++ and
Python. The closer to Lua that some concept may seem, the more
confusing it becomes.
> If this were present in Lua then having an assignment to the result of a
> function call would be really easy. Since tables are passed by reference
This statement is a good example of the sort of misunderstanding
that come from knowing too much C++ and too little Lua.
Everything in Lua is a TValue structure. In the case of simple,
fixed-length objects like booleans and numbers, the actual data
is stored in the structure itself. In the case of mutable or variable-length
objects like strings, tables and userdata, a pointer to the object is
passed.
> a function could return the table. Then the assignment operator
> could be invoked on it.
There is no such thing as an assignment operator. There is
an assignment statement. Even that name is not used in the
syntax definition, all it says is that
varlist ‘=’ explist
local namelist [‘=’ explist]
are possible forms of a statement.
> local tbl = {}
> tbl.value = 0
>
> mt = { __assign(value) = function(value) tbl.value = value end}
The above is total gibberish.
'__assign(value)' in some way suggests that 'value' is a parameter,
but in 'function(value) tbl.value = value end' the field name 'value'
and the parameter 'value' both appear.
Listen, Abhijit. Three experienced and very friendly members
of the Lua community have told you in the nicest possible way
that your idea is crazy and incompatible with Lua. You have
come back with yes-buts every time. This is not acceptable.
Rather divert your obvious energy and enthusiasm into a study
of the Lua API. Once you understand what makes the Lua engine
tick, you will be in a better position to tune it.