lua-users home
lua-l archive

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


Hi!

As I'm working on the engine for JLua I often see that I
haven't quite understood Lua's semantics completely...

In this case I'm having some trouble with upvalues. I
can see how they work in general... But when any block
can have local variables, things get a little confusing.

Is it so that upvalues only "care" about functions?
I.e. if I put an upvalue in another kind of block it
wouldn't get its value from the function enclosing
that block, but from the function enclosing the
function in which the block is found? (Even if the
block itself has local variables etc?)

And if the function in which the upvalue is found is
defined within a block with local variables, will they
shadow the local variables of the enclosing function?

When I tested the latter, the block did indeed shadow
the function:

function f()
  local x=1
  do
    local x=2
    function g()
      print(%x)
    end
  end
  g()
end

If there is no local declaration in the explicit block,
then the value local to the function is used. But if
that block is changed into a function, an error message
occurs...

 "upvalue must be global or local to immediately
  outer scope"

This sounds reasonable... But what, then, is meant by
"scope"? Is a block which is not a function only treated
as a scope if it has a local declaration of the
variable in question, but functions are always treated
as scopes?

I mean, even if I changed "local x=2" to "local y=2"
in the above, I get no error message, which would
suggest that it is not treated as a scope, but it
clearly *is* when the variable is shadowed... Or
is that then just a special case of the function scope?

Hm. That ought to be enough ranting for today.

</slightly bewildered>

- M :)

--

  Magnus Lie Hetland      (magnus at hetland dot org)

 "Reality is what refuses to disappear when you stop
  believing in it"                 -- Philip K. Dick