404 posts
Location
St. Petersburg
Posted 31 January 2013 - 01:28 PM
consider the code:
local bError = true
local obj = setmetatable({},{
__index = function(self,index)
bError = false
return 1
end,
__newindex = function(self,index,value)
if bError
then error("attempt to edit a read-only table")
else
self[index] = value
bError = true
end
end,
__metatable = true
})
now consider this:
obj.val = (obj.val + 1)
will the __index or __newindex be called first?
808 posts
Posted 31 January 2013 - 01:35 PM
consider the code:
local bError = true
local obj = setmetatable({},{
__index = function(self,index)
bError = false
return 1
end,
__newindex = function(self,index,value)
if bError
then error("attempt to edit a read-only table")
else
self[index] = value
bError = true
end
end,
__metatable = true
})
now consider this:
obj.val = (obj.val + 1)
will the __index or __newindex be called first?
Under conventional programming, it should evaluate the expression on the right, store that in a register, and move that to a variable's location. So theoretically, __index should be called first. So supposedly reading once should enable one write, and the two can be combined to one statement. Cool trick. Tested this yet?
404 posts
Location
St. Petersburg
Posted 31 January 2013 - 01:42 PM
–snippity snip–
nope. i don't feel like opening up minecraft just to test it. just thought it was an interesting concept
7508 posts
Location
Australia
Posted 31 January 2013 - 01:44 PM
–snippity snip–
nope. i don't feel like opening up minecraft just to test it. just thought it was an interesting concept
Then open cc-emulator :P/> opens much quicker
3790 posts
Location
Lincoln, Nebraska
Posted 31 January 2013 - 02:21 PM
6 posts
Posted 31 January 2013 - 02:23 PM
treu I lov cc-emu! :)/> scuh a good progarm!
1054 posts
Posted 31 January 2013 - 02:38 PM
__index should definitely be called first as ElvishJerricco said as well. It's not just that right hand expressions are evaluated first. It's rather that the result of the right hand expression in this case is an r-value and r-values are temporary objects that have no designated address. So they will be calculated and passed directly to the next function (__newindex in this case). Lua doesn't really make use of l-values nor r-values, but the theory stays the same. Though, it's not usually stored in a register as ElvishJerricco said, but rather in RAM until the function call ended. (Imagine strings being stored in registers…)
Edit: slightly off-topic.. Programming languages that are syntactically valid (thus are a
context-free language [
except for perl :o/>]) could never be syntactically paradoxical. They can be ambiguous, but not paradoxical. Semantically however, they can be, but that's most often a matter of interpretation and a way of looking at things. There are the logical programming paradoxes of course, but those are mathematical and that's not relevant to this question. In the end, every statement in every context-free programming language has to have a deterministic outcome, so can't be paradoxical. (Note: as I barely give any sources, look at this as my personal opinion)
392 posts
Location
Christchurch, New Zealand
Posted 31 January 2013 - 02:51 PM
You can store a pointer to a string in a register :D/>
1054 posts
Posted 31 January 2013 - 02:57 PM
You can store a pointer to a string in a register :D/>
Lua doesn't pass strings by reference, right? Hence my example (numbers could be stored in registers and tables and functions are passed by reference).
Edit: And before you mention it, they will probably be stored as a pointer in a register at some point, but definitely in RAM on entering the called function. (I know you where thinking it ;)/> )
3 posts
Posted 31 January 2013 - 04:37 PM
One more post so i can post my annoying bug
I truly don't mean to spam, but i need help and i can't get it unless i get approved posts, which i find really annoying to do as i have a very unique issue.
1054 posts
Posted 31 January 2013 - 04:45 PM
One more post so i can post my annoying bug
I truly don't mean to spam, but i need help and i can't get it unless i get approved posts, which i find really annoying to do as i have a very unique issue.
Try reading a very prominent sticky with a very prominent title in the very section you should post your question.
2217 posts
Location
3232235883
Posted 02 February 2013 - 11:56 AM
__index isnt called necicarily because lua looks for the assignment operator before doing that
however if you do something like this
table[val1][val2]
table will be indexed with val1
1054 posts
Posted 02 February 2013 - 11:00 PM
__index isnt called necicarily because lua looks for the assignment operator before doing that
however if you do something like this
table[val1][val2]
table will be indexed with val1
That makes no sense. The assignment operator will be used if there is no __newindex metamethod. And both __index and __newindex are implemented in his example, so they will definitely be used.