luciano de souza | 27 Jun 2012 08:03
Picon

Defining a property with classlib

Hello all,

I am trying to implement a property with classlib, but the code does not work:

require('classlib')

class.number()

function number:__init()
local mt = getmetatable(self)

function mt.__index(t, k)
if k == 'value' then
return math.random(1000)
end
end
end

number = number()
print(number.value)

In stead of showing a ramdomic number, classlib raises an error.

This code works:

setmetatable(_G, {__index = function (t, k) return math.random(1000) end})

I would like to to the same but in the class scope. For this reason, I
tried also:

(Continue reading)

Alexander R. | 27 Jun 2012 08:58

A question on the definition of a chunk

Hello all,

after reading through "Programming in Lua" I started to read it a second time, paying more attention to the details right now. The chapter 1.1 [1] explicates the concept of chunks and I'm not quite sure that I understand everything absolutely right. It says:
> Each piece of code that Lua executes, such as a file or a single line in interactive mode, is a chunk. More specifically, a >chunk is simply a sequence of statements.
I'm afraid it's a language problem as my English is not perfect, but do I get this right: A chunk in Lua is everything, each piece of code that gets executed by Lua is called so, no matter if it's a 50-line-function or a single association, et cetera?

Thanks in advance,
~|Alex

[1] http://www.lua.org/pil/1.1.html
Michal Kottman | 27 Jun 2012 10:06
Picon
Gravatar

Re: A question on the definition of a chunk

On 27 June 2012 08:58, Alexander R. <dotnot <at> lavabit.com> wrote:
> I'm afraid it's a language problem as my English is not perfect, but do I
> get this right: A chunk in Lua is everything, each piece of code that gets
> executed by Lua is called so, no matter if it's a 50-line-function or a
> single association, et cetera?

Each "thing" you compile using the "load" family of functions - i.e.
luaL_loadbuffer, luaL_loadstring, luaL_loadfile from the C API and
load, loadfile, loadstring from the Lua API - is understood as a
"chunk" and is compiled as if it were a function (without the
"function (...)" header). You can even pass parameters to the chunk
and return values from it:

--- somefile.lua
local paramA, paramB = ...
return paramA + paramB

How big is a chunk is depending on how (often) you call the load*
function - you can either load entire file in one go - then the whole
file is one chunk - or you can compile each line (or series of lines)
as the Lua interpreter does - then each line is its own chunk, and any
locals you define have scope only in that chunk - that line.

Dirk Laurie | 27 Jun 2012 11:32
Picon

Re: A question on the definition of a chunk

2012/6/27 Michal Kottman <k0mpjut0r <at> gmail.com>:
> On 27 June 2012 08:58, Alexander R. <dotnot <at> lavabit.com> wrote:
>> I'm afraid it's a language problem as my English is not perfect, but do I
>> get this right: A chunk in Lua is everything, each piece of code that gets
>> executed by Lua is called so, no matter if it's a 50-line-function or a
>> single association, et cetera?
>
> Each "thing" you compile using the "load" family of functions - i.e.
> luaL_loadbuffer, luaL_loadstring, luaL_loadfile from the C API and
> load, loadfile, loadstring from the Lua API - is understood as a
> "chunk" and is compiled as if it were a function (without the
> "function (...)" header).

Way I see it, a character string `P` is a chunk if and only if the first
return value of

        loadstring(P)

is not nil, otherwise `P` is garbage.  Whether it is just one statement
or a 5000-line program.  ("garbage" and "chunk" may differ by only one
character.) (It is `load(P)` in 5.2 but for the moment `loadstring`
still works.)

TNHarris | 27 Jun 2012 12:41
Favicon
Gravatar

Re: A question on the definition of a chunk

On Wednesday, June 27, 2012 02:58:26 AM Alexander R. wrote:
> I'm afraid it's a language problem as my English is not perfect, but do
> I get this right: A chunk in Lua is everything, each piece of code that
> gets executed by Lua is called so, no matter if it's a 50-line-function
> or a single association, et cetera?

The building blocks of Lua are functions. All functions can be referenced, 
where you cannot make a reference to an arbitrary statement. A chunk is just a 
specific type of function: one which is generated from a complete pass of the 
parser over an input source. (Not necessarily a file or string, since the 
load() function can accept a function as a source.)

The significance of a chunk, compared to a regular function, is that a chunk 
is not created in the context of another function. This is relevant to where 
global variables come from.

--

-- 
tom <telliamed <at> whoopdedo.org>

Gavin Wraith | 27 Jun 2012 13:09

Re: A question on the definition of a chunk

In message <201206270641.27803.telliamed <at> whoopdedo.org> you wrote:

> On Wednesday, June 27, 2012 02:58:26 AM Alexander R. wrote:
> > I'm afraid it's a language problem as my English is not perfect, but do
> > I get this right: A chunk in Lua is everything, each piece of code that
> > gets executed by Lua is called so, no matter if it's a 50-line-function
> > or a single association, et cetera?

This turns on what you mean by "piece of code" and "executed". As the
manual says, function definitions, looping structures, do ... end
structures and separate files of Lua source code constitute chunks.
Chunks contain sequences of statements or chunks. The notion of
subchunk, i.e. one chunk contained in another, is the salient concept
to be grasped, IMHO. Chunks form an ordered tree (in the mathematical sense
of that phrase) with root the chunk consisting of the whole program. 
The scope of a local variable is the part of the smallest chunk containing 
its declaration which follows it.
--

-- 
Gavin Wraith (gavin <at> wra1th.plus.com)
Home page: http://www.wra1th.plus.com/

Dirk Laurie | 27 Jun 2012 17:56
Picon

Re: A question on the definition of a chunk

> The scope of a local variable is the part of the smallest chunk
> containing its declaration which follows it.

The manual does not bring chunks into the definition of scope.
It says:

   The scope of variables begins at the first statement
   after their declaration and lasts until the end of
   the innermost block that includes the declaration.

Is the quoted definition equivalent to that?  Hardly.

The "smallest chunk containing its declaration" of a variable
is just the declaration.  The part of it that follows the
declaration is empty.  It's useless, but it is a chunk.

An example, showing that those are chunks.

   f=load"local y=x"; print(f)
   f=load""; print(f)

In both cases, f is a perfectly respectable function,
the execution which returns nothing and has no side effects.

Gavin Wraith | 27 Jun 2012 18:47

Re: A question on the definition of a chunk

In message <CABcj=tnG6e_zFBv+FgxR-jkd8xOCxkd8SA-WNUXAfj9mL+_NLg <at> mail.gmail.com> you wrote:

> > The scope of a local variable is the part of the smallest chunk
> > containing its declaration which follows it.
> 
> The manual does not bring chunks into the definition of scope.
> It says:
> 
>    The scope of variables begins at the first statement
>    after their declaration and lasts until the end of
>    the innermost block that includes the declaration.
> 
> Is the quoted definition equivalent to that?  Hardly.
> 
> The "smallest chunk containing its declaration" of a variable
> is just the declaration.  The part of it that follows the
> declaration is empty.  It's useless, but it is a chunk.

Apologies. All this time I have been equating "chunk" with "block".
So is it correct to say that the scope of a local variable is the
part of its smallest enclosing block that follows it?

I had better replace "(sub)chunk" with "(sub)block" in all my
documentation. 
--

-- 
Gavin Wraith (gavin <at> wra1th.plus.com)
Home page: http://www.wra1th.plus.com/

liam mail | 27 Jun 2012 19:06

Re: A question on the definition of a chunk

> Apologies. All this time I have been equating "chunk" with "block".
No need to apologise [1]

[1] http://www.lua.org/manual/5.2/manual.html#3.3.2
"The unit of execution of Lua is called a chunk. Syntactically, a
chunk is simply a block:
	chunk ::= block"

--Liam

Roberto Ierusalimschy | 27 Jun 2012 19:15
Picon
Picon

Re: A question on the definition of a chunk

> [1] http://www.lua.org/manual/5.2/manual.html#3.3.2
> "The unit of execution of Lua is called a chunk. Syntactically, a
> chunk is simply a block:
> 	chunk ::= block"

The fact that a chunk is a block does not mean that any block is a
chunk. Chunks do not nest (unlike blocks). A chunk is an outermost block
which you feed to "load".

-- Roberto

Gavin Wraith | 27 Jun 2012 20:07

Re: A question on the definition of a chunk

In message <20120627171519.GB6735 <at> inf.puc-rio.br> you wrote:

> The fact that a chunk is a block does not mean that any block is a
> chunk. Chunks do not nest (unlike blocks). A chunk is an outermost block
> which you feed to "load".

I had better rephrase my previous email. So:

OK, the concept for which I had been wrongly using these words is one
for which I am ignorant of the right terminology. I think I mean a
subblock that is maximal with the property that were an
enclosing block to be preceded by a local declaration the local 
variable would be in scope in the whole subblock.

So for example in

do x = a(); y = b() end

the subblocks x = a() and y = b() are not maximal, even though
they are in the scope of foo in 

  do local foo = 57;  x = a(); y = b() end

On the other hand x = a(); y = b() is maximal.

Is this making any sense?

--

-- 
Gavin Wraith (gavin <at> wra1th.plus.com)
Home page: http://www.wra1th.plus.com/

Roberto Ierusalimschy | 27 Jun 2012 20:49
Picon
Picon

Re: A question on the definition of a chunk

> OK, the concept for which I had been wrongly using these words is one
> for which I am ignorant of the right terminology. I think I mean a
> subblock that is maximal with the property that were an
> enclosing block to be preceded by a local declaration the local 
> variable would be in scope in the whole subblock.
> 
> So for example in
> 
> do x = a(); y = b() end
> 
> the subblocks x = a() and y = b() are not maximal, even though
> they are in the scope of foo in 
> 
>   do local foo = 57;  x = a(); y = b() end
> 
> On the other hand x = a(); y = b() is maximal.
> 
> Is this making any sense?

I do not think so. It is the same problem: blocks are lists of
statements, but that does not mean that any list of statements is a
block. If you draw the syntax tree of the above piece of code, it
becomes clear that "x = a()" (in that particular place) is not a
block. So, that occurence of "x = a()" cannot be a subblock.

-- Roberto

Gavin Wraith | 27 Jun 2012 21:12

Re: A question on the definition of a chunk

In message <20120627184905.GA7463 <at> inf.puc-rio.br> you wrote:

> > Is this making any sense?
> 
> I do not think so. It is the same problem: blocks are lists of
> statements, but that does not mean that any list of statements is a
> block. If you draw the syntax tree of the above piece of code, it
> becomes clear that "x = a()" (in that particular place) is not a
> block. So, that occurence of "x = a()" cannot be a subblock.

OK. Reduction is not symmetric :). So, for checking that I have
it right, am I correct in saying that a local variable's scope 
consists of the statements or blocks that follow its declaration 
that also lie within the smallest block that contains its
declaration? 

--

-- 
Gavin Wraith (gavin <at> wra1th.plus.com)
Home page: http://www.wra1th.plus.com/

Andrew Starks | 27 Jun 2012 21:48
Favicon
Gravatar

Re: A question on the definition of a chunk

On Wed, Jun 27, 2012 at 2:12 PM, Gavin Wraith <gavin <at> wra1th.plus.com> wrote:
> In message <20120627184905.GA7463 <at> inf.puc-rio.br> you wrote:
>
>> > Is this making any sense?
>>
>> I do not think so. It is the same problem: blocks are lists of
>> statements, but that does not mean that any list of statements is a
>> block. If you draw the syntax tree of the above piece of code, it
>> becomes clear that "x = a()" (in that particular place) is not a
>> block. So, that occurence of "x = a()" cannot be a subblock.
>
> OK. Reduction is not symmetric :). So, for checking that I have
> it right, am I correct in saying that a local variable's scope
> consists of the statements or blocks that follow its declaration
> that also lie within the smallest block that contains its
> declaration?
>
> --
> Gavin Wraith (gavin <at> wra1th.plus.com)
> Home page: http://www.wra1th.plus.com/
>
I may have this wrong, but...

Is it best to view chunks as an implementation detail that is relevant
only when you are loading them? That is, Lua can be thought to
more-or-less process (compile into byte code) chunks at a time, and
that is why you might care about them, which is to say, when syntax
errors caught by the parser would show up.

Scoping considerations, such as environment levels, closures, globals,
etc., are a feature of the language which probably doesn't map
perfectly to how Lua would process chunks of code that it reads in.

Best Regards,

Andrew Starks

Sean Conner | 27 Jun 2012 21:52
Favicon

Re: A question on the definition of a chunk

It was thus said that the Great Gavin Wraith once stated:
> In message <20120627184905.GA7463 <at> inf.puc-rio.br> you wrote:
> 
> > > Is this making any sense?
> > 
> > I do not think so. It is the same problem: blocks are lists of
> > statements, but that does not mean that any list of statements is a
> > block. If you draw the syntax tree of the above piece of code, it
> > becomes clear that "x = a()" (in that particular place) is not a
> > block. So, that occurence of "x = a()" cannot be a subblock.
> 
> OK. Reduction is not symmetric :). So, for checking that I have
> it right, am I correct in saying that a local variable's scope 
> consists of the statements or blocks that follow its declaration 
> that also lie within the smallest block that contains its
> declaration? 

Here's two examples of Lua chunks (sans error checking, which would cloud
the issue), compiled via loadstring() (which returns a function that when
run, will execute the code in the given chunk):

	c1 = loadstring [[
		-- this is chunk # 1
		local a	-- this is local to this chunk
		a = 15

		-- this function will be loaded into the default
		-- environment.  In most cases, this will end up
		-- in the global name space.

		function f(x)
		  return 3 * x + a
		end
	]]
	c1()	-- run the loaded chunk

	c2 = loadstring [[
		-- this is chunk #2
		-- this function is local only to this chunk

		local function y(i)
		  return i * a
		end

		-- the 'a' above does not refer to the 'a' defined in the
		-- previous chunk, as that was local to that chunk.  In this
		-- case, y() will default to an 'a' in the global name
		-- space.  If it exists, this will run, otherwise it will
		-- generate an error.

		y(3)
	]]

	-- at this point, c2 is defined, but what happens next ..

	c2()

If 'a' doesn't exist, then running c2() will result in:

[string "                -- this is chunk #2..."]:5: attempt to perform
arithmetic on global 'a' (a nil value)
stack traceback:
        [string "                -- this is chunk #2..."]:5: in function 'y'
        [string "                -- this is chunk #2..."]:14: in function
'c2'
        stdin:1: in main chunk
        [C]: ?

But doing this:

	a = 3
	c2()

yields no errors.

  -spc (Does that help clarify the issue?)

Roberto Ierusalimschy | 27 Jun 2012 22:09
Picon
Picon

Re: A question on the definition of a chunk

> OK. Reduction is not symmetric :). So, for checking that I have
> it right, am I correct in saying that a local variable's scope 
> consists of the statements or blocks that follow its declaration 
> that also lie within the smallest block that contains its
> declaration? 

That is not too different from the manual:

  The scope of a local variable begins at the first statement after
  its declaration and lasts until the last non-void statement
  of the innermost block that includes the declaration.

-- Roberto

Gavin Wraith | 27 Jun 2012 22:45

Re: A question on the definition of a chunk

In message <20120627200956.GA7808 <at> inf.puc-rio.br> you wrote:

> That is not too different from the manual:
> 
>   The scope of a local variable begins at the first statement after
>   its declaration and lasts until the last non-void statement
>   of the innermost block that includes the declaration.

Thanks to everybody who replied. I think I have it sorted now. I was
sometimes mistakenly using the word 'chunk' when I should have said
'block'. 

--

-- 
Gavin Wraith (gavin <at> wra1th.plus.com)
Home page: http://www.wra1th.plus.com/

Gavin Wraith | 27 Jun 2012 19:58

Re: A question on the definition of a chunk

In message 
<CAOMMs2SekyK5KiJ6wkmULudodpgvbaMsrRBbwi5T5OXiqze05A <at> mail.gmail.com> 
you wrote:

> > Apologies. All this time I have been equating "chunk" with "block".
> No need to apologise [1]

> [1] http://www.lua.org/manual/5.2/manual.html#3.3.2
> "The unit of execution of Lua is called a chunk. Syntactically, a
> chunk is simply a block:
> chunk ::= block"

OK, the concept for which I had been wrongly using these words is one
for which I am ignorant of the right terminology. I think I mean a
subchunk that is maximal with the property that were an
enclosing chunk to be preceded by a local declaration the local 
variable would be in scope in the whole subchunk.

So for example in

do x = a(); y = b() end

the subchunks x = a() and y = b() are not maximal, even though
they are in the scope of foo in 

  do local foo = 57;  x = a(); y = b() end

Is this making any sense?

--

-- 
Gavin Wraith (gavin <at> wra1th.plus.com)
Home page: http://www.wra1th.plus.com/


Gmane