This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
tesla1889's profile picture

i think i may have found a paradox :D

Started by tesla1889, 31 January 2013 - 12:28 PM
tesla1889 #1
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?
ElvishJerricco #2
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?
tesla1889 #3
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
theoriginalbit #4
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
Cranium #5
Posted 31 January 2013 - 02:21 PM
http://cc-emu.afterlifelochie.net/
Try that…. So much more fun.
Vladious #6
Posted 31 January 2013 - 02:23 PM
treu I lov cc-emu! :)/> scuh a good progarm!
Orwell #7
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)
NeverCast #8
Posted 31 January 2013 - 02:51 PM
You can store a pointer to a string in a register :D/>
Orwell #9
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 ;)/> )
giakomo1201 #10
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.
Orwell #11
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.
PixelToast #12
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
Orwell #13
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.