Codea crashes if I try ...

I was looking for a kind of Option Explicit replacement (like in VBA) for Codea and found:

local declaredNames = {}
    function declare (name, initval)
      rawset(_G, name, initval)
      declaredNames[name] = true
    setmetatable(_G, {
      __newindex = function (t, n, v)
        if not declaredNames[n] then
          error("attempt to write to undeclared var. "..n, 2)
          rawset(t, n, v)   -- do the actual set
      __index = function (_, n)
        if not declaredNames[n] then
          error("attempt to read undeclared var. "..n, 2)
          return nil

I am not quite what the code does … but I crashes Codea if I try to run it …

That’s an interesting piece of code.

It creates a requirement to call declare( name, value ) to declare a global variable.

However Codea creates and reads from global variables every frame — parameter.*, ElapsedTime and WIDTH / HEIGHT are some examples. So when Codea tries to read from a global (parameters are an example of this) it is not getting a value.

I’ll try to prevent it from crashing in this case.

@Simeon: Thank you. A pice of code didnt work and I nearly bit into ipad whan I found the error after a long time ( peece instead of peace damn my bad eyesight X( )
That was the motivation for my search …