Documentation

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Documentation

Philippe Lhoste-2
I am trying to understand how to use properly the t:f() syntax, and how it is 
useful, mostly looking at other people code. Unfortunately still scarse on the 
Net.
while rereading the Lua 4.0 manua, I have meet a potential discrepancy:

In section 3, you say "The form t:f(x) is syntactic sugar for t.f(t,x), which 
calls the method f from the table t passing the table itself as the first 
parameter (see Section 4.5.9).".

In section 4.5.9, you say "The statement function v:f (...) ... end is just 
syntactic sugar for v.f = function (self, ...) ... end
Note that the function gets an extra formal parameter called self."

Perhaps it is intended, but I find it a bit confusing...
It seems that t is available as global variable in f, anyway.
So I bet it is the section 4.5.9 which is correct.

Also the status of "self" and "arg" variables are a bit ambiguous. They are 
not reserved words, nor internal variables (as defined in section 4.1) because 
they have no underscore prefix, nor capital names. I suppose they are actually 
local variables.
What I am trying to explain is... Well, I am not sure. Perhaps you should 
stress out the fact that they are special, eg. in the 4.1 section, where you 
give some conventions.

Are _INPUT, _OUTPUT, _STDIN, _STDOUT, _STDERR, _PROMPT, _ERRORMESSAGE, _ALERT 
the only internal variables, or are there others? Most of them are defined in 
libraries, can we create our internal variables in our libraries, using the 
same convention? Any risk of conflict?

Sorry for the dumb questions, I am still learning, and certainly on a par with 
the gentlemen discuting coroutines or OO...

Regards.


Regards.

¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
http://philho.multimania.com/
_.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._


Reply | Threaded
Open this post in threaded view
|

Re: Documentation

Reuben Thomas-3
> In section 3, you say "The form t:f(x) is syntactic sugar for t.f(t,x), which
> calls the method f from the table t passing the table itself as the first
> parameter (see Section 4.5.9).".

> In section 4.5.9, you say "The statement function v:f (...) ... end is just
> syntactic sugar for v.f = function (self, ...) ... end
> Note that the function gets an extra formal parameter called self."

You're just confusing formal and actual parameters: if you define a function
as:

v:f () ... end

you actually get

v.f = function (self) ... end

and then when you call it with v:f(), the actual call made is v:f(v), so
self gets set to v within f.

-- 
http://sc3d.org/rrt/ | certain, a.  insufficiently analysed


Reply | Threaded
Open this post in threaded view
|

Re: Documentation

David Jones
In message <Pine.LNX.4.30.0012111228100.17116-100000@...>, Re
uben Thomas writes:
> > In section 3, you say "The form t:f(x) is syntactic sugar for t.f(t,x), whi
> ch
> > calls the method f from the table t passing the table itself as the first
> > parameter (see Section 4.5.9).".
> 
> > In section 4.5.9, you say "The statement function v:f (...) ... end is just
> > syntactic sugar for v.f = function (self, ...) ... end
> > Note that the function gets an extra formal parameter called self."
> 
> You're just confusing formal and actual parameters: if you define a function
> as:

No the manual draws the correct distinction.

> v:f () ... end
> 
> you actually get
> 
> v.f = function (self) ... end

IE the function is declared with an extra formal parameter called self.
> 
> and then when you call it with v:f(), the actual call made is v:f(v), so
> self gets set to v within f.

IE v:f() generates a call to f with an extra actual parameter (the value
of v).  This actual parameter doesn't have to be bound to the formal
parameter self.  I could declare my function:

v.f = function (a, b, c, self) ... end

Though that would probably be confusing.

Conversely, I don't have to call a function that I declared function
v:f() using the v:f() syntax.

Cheers,
 djones

Reply | Threaded
Open this post in threaded view
|

RE: Documentation

Philippe Lhoste-2
In reply to this post by Philippe Lhoste-2
> Sorry for the dumb questions, I am still learning, and certainly
> on a par with
> the gentlemen discuting coroutines or OO...

Ouch, that's "NOT on a par with"...

Reuben Thomas wrote:
> > In section 3, you say "The form t:f(x) is syntactic sugar for t.f(t,x), 
which
> > calls the method f from the table t passing the table itself as the first
> > parameter (see Section 4.5.9).".
>
> > In section 4.5.9, you say "The statement function v:f (...) ... end is 
just
> > syntactic sugar for v.f = function (self, ...) ... end
> > Note that the function gets an extra formal parameter called self."
>
> You're just confusing formal and actual parameters: if you define a function
> as:
>
> v:f () ... end
>
> you actually get
>
> v.f = function (self) ... end
>
> and then when you call it with v:f(), the actual call made is v:f(v), so
> self gets set to v within f.

OK, I didn't read attentively enough, I didn't saw the first paragraph was 
about calling the function, while the second one was about defining the 
function... So the question is indeed dumb...

Thank you.

¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯
Philippe Lhoste (Paris -- France)
Professional programmer and amateur artist
http://jove.prohosting.com/~philho/
http://philho.multimania.com/
_.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._.·´¯`·._