Questions from a new Lua user...

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

Questions from a new Lua user...

Eric Tetz-2
Hello!

I'm a new Lua user, and would like to congratulate it's authors on creating such a nice little
package. I'm replacing the scripting language in a Windows multimedia authoring app
(http://www.mediachance.com) and Lua is my top contender so far.

I have a couple questions, please forgive me if they are totally naive...

· Much of the real-world Lua code I've seen requires a function to copy a table.  I had planned to
include a clone() function (written in C) for my users, but I am worried that there is a good
reason one doesn't already exist in the standard library.  Is there?

· I originally discovered that Lua discards all but the first return value of a function when used
in an argument list (unless it's the last or only arg) by accident, when trying to do something
like:

   drawtext (pos:xy(), text)

where xy() returns the x and y of a Point. It seems to me that using all the return values would
be what one would expect.  Discarding values in this one situation seems inconsistent. Why the
special case? Does it make Lua's implementation easier? Are there idioms where this is genuinely
useful?  Any chance this will change in future versions?

· I'm trying to find examples of using object-oriented syntax to manipulate objects in the host
app.  Most of the stuff I've found online is for Lua 3.x.  Can someone direct me to a
straightforward example of exporting class hierarchies (rather than function libraries) to Lua
4.0?

Cheers,
Eric



__________________________________________________
Do You Yahoo!?
Yahoo! Messenger - Talk while you surf!  It's FREE.
http://im.yahoo.com/

Reply | Threaded
Open this post in threaded view
|

Re: Questions from a new Lua user...

Steve Dekorte-6
Eric Tetz wrote:
> * Much of the real-world Lua code I've seen requires a function to copy a table.  I planned to 
> include a clone() function (written in C) for my users, but I am worried that there is a good 
> reason one doesn't already exist in the standard library.  Is there? 

I don't know for sure, but it may be because there are a bunch of different ways to clone a table. (deep, shallow, etc)

Steve

Reply | Threaded
Open this post in threaded view
|

Re: Questions from a new Lua user...

Roberto Ierusalimschy
In reply to this post by Eric Tetz-2
> It seems to me that using all the return values would be what one would 
> expect. Discarding values in this one situation seems inconsistent. 
> Why the special case?

We would say that the normal case is to keep only one value, and the 
special case is to allow multiple values when the function is the last (or 
the only) one in a list ;-) In Lua, the same function may return different 
number of values; so, it would be very difficult to understand something 
like 

  a,b,c = f(), g()

if f() can (dynamically) return 1 or 2 values.


> Does it make Lua's implementation easier?

Yes.

> Are there idioms where this is genuinely useful?

Not that I know. But if you think that the special case is when Lua keeps
multiple returns, then for sure there are many useful idioms (for instance 
«assert(readfrom"name")») 


> Any chance this will change in future versions?

Anything might change in the future, but we are happy with the current
semantics.


-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Questions from a new Lua user...

David Jeske-3
In reply to this post by Eric Tetz-2
On Wed, Oct 18, 2000 at 01:24:29AM -0700, Eric Tetz wrote:
> ? I originally discovered that Lua discards all but the first return value of a function when used
> in an argument list (unless it's the last or only arg) by accident, when trying to do something
> like:
> 
>    drawtext (pos:xy(), text)
> 
> where xy() returns the x and y of a Point. 

Interesting. This is a very python-esq concept. In Python, multiple
return arguments are tuples, and they are conceptually
interchangable. In Python you could do:

class Position:
  def __init__(self,x,y):
    self.x = x
    self.y = y
  def xy(self):
    return (self.x,self.y)

pos = Position(4,5)
a_point = pos.xy()       # pso = (4,5)
x,y = a_point            # x = 4, y = 5

x,y = pos.xy()           # x = 4, y = 5

Then, with drawtext, you could do:

def drawtext((x,y), text):
  # do some work to draw your text


drawtext((2,3), "draw some text at 2,3")
drawtext(a_point,"draw some text at a_point")
drawtext(pos.xy(),"draw some text at pos.xy()")

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Questions from a new Lua user...

Eric Tetz-2
In reply to this post by Eric Tetz-2
--- Roberto Ierusalimschy <[hidden email]> wrote:
> We would say that the normal case is to keep only one value, and the 
> special case is to allow multiple values when the function is the last (or 
> the only) one in a list ;-) In Lua, the same function may return different 
> number of values; so, it would be very difficult to understand something 
> like 
> 
>   a,b,c = f(), g()
> 
> if f() can (dynamically) return 1 or 2 values.

Ahh, that's the "idiom where this is useful" that I was wondering about.  It makes sense, seeing
your example, that the only valid place for a function that can dynamically return different
numbers of args would be at the end of the argument list.

> > Are there idioms where this is genuinely useful?
> 
> Not that I know. But if you think that the special case is when Lua keeps
> multiple returns, then for sure there are many useful idioms (for instance 
> «assert(readfrom"name")») 

I was thinking the special case was when Lua discarded return values.  It's easy to see how not
discarding them is useful.  Now I see why sometimes discarding them is simply necessary.

Thanks,
Eric

__________________________________________________
Do You Yahoo!?
Yahoo! Messenger - Talk while you surf!  It's FREE.
http://im.yahoo.com/

Reply | Threaded
Open this post in threaded view
|

Re: Questions from a new Lua user...

Eric Tetz-2
In reply to this post by Eric Tetz-2
--- David Jeske <[hidden email]> wrote:
> Interesting. This is a very python-esq concept. In Python, multiple
> return arguments are tuples, and they are conceptually
> interchangable. In Python you could do:
> 
> class Position:
>   def __init__(self,x,y):
>     self.x = x
>     self.y = y
>   def xy(self):
>     return (self.x,self.y)
> 
> pos = Position(4,5)
> a_point = pos.xy()       # pso = (4,5)
> x,y = a_point            # x = 4, y = 5
> 
> x,y = pos.xy()           # x = 4, y = 5
> 
> Then, with drawtext, you could do:
> 
> def drawtext((x,y), text):
>   # do some work to draw your text
> 
> 
> drawtext((2,3), "draw some text at 2,3")
> drawtext(a_point,"draw some text at a_point")
> drawtext(pos.xy(),"draw some text at pos.xy()")

I really like that usage.  I like being able to code drawtext as:

  drawtext(int,int,string)

which is more flexible interface than

   drawtext(point,string)

and still being able to call it as

  drawtext(p:xy(),s)
  
Or being able to do say something succinct like

  drawrect(r:bounds(),clr:rgb())

instead of

  drawrect(r.tl.x, r.tl.y, r.br.x, r.br.y, clr.r, clr.g, clr.b).
   
To bad. :(

Apparently Lua's doesn't allow this because:
   A) it's easier to implement if you discard values
   B) it would be confusing when used with functions that don't always return the same number of
values

Personally I would rather have all return values used all the time.  Potential confusion could be
avoided with a little discipline (as with other Lua features), and you would really be able to
*use* this unique feature of Lua.
   
Cheers,
Eric

__________________________________________________
Do You Yahoo!?
Yahoo! Messenger - Talk while you surf!  It's FREE.
http://im.yahoo.com/

Reply | Threaded
Open this post in threaded view
|

Re: Questions from a new Lua user...

Jonathan Adamczewski
On Wed, 18 Oct 2000, Eric Tetz wrote:

> 
> and still being able to call it as
> 
>   drawtext(p:xy(),s)
>   

In this case, call as 
drawtext(s,p:xy()) 
and drawtext gets all three values.  Just change the ordering of names in
the function.


> Or being able to do say something succinct like
> 
>   drawrect(r:bounds(),clr:rgb())
> 
> instead of
> 
>   drawrect(r.tl.x, r.tl.y, r.br.x, r.br.y, clr.r, clr.g, clr.b).
>    

Use tables?  (the following stuff is very unchecked)

function bounds(a)
	local t = {x=1,y=1}
	local b = {x=10,y=10}
	return {tl=t,br=b}
end

r = {bounds=bounds}

drawrect(r:bounds(),...) --whatever

Gives a table containing the four values to drarect.

By using tables and changing the order of arguments, I believe you can do
all the things you have mentioned.

(of coure I could have missed the point - please be gentle pointing out
mistakes... I started with lua just over a week ago :)

Jonathan.


Reply | Threaded
Open this post in threaded view
|

Re: Questions from a new Lua user...

David Jeske-3
In reply to this post by Eric Tetz-2
On Wed, Oct 18, 2000 at 02:04:53PM -0700, Eric Tetz wrote:
> > def drawtext((x,y), text):
> >   # do some work to draw your text
> > 
> > 
> > drawtext((2,3), "draw some text at 2,3")
> > drawtext(a_point,"draw some text at a_point")
> > drawtext(pos.xy(),"draw some text at pos.xy()")
> 
> I really like that usage.  I like being able to code drawtext as:
> 
>   drawtext(int,int,string)

Notice that in Python, it's not (int, int, string), it's
(tuple,string) where the tuple is just declared with the function
prototype as (x,y), yeilding the argument signature ((x,y),text). 

> which is more flexible interface than
> 
>    drawtext(point,string)

Actually, in python, the function IS drawtext(point,string), in fact
the two functions are exactly identical:

def drawtext(point,string):
  x,y = point

def drawtext((x,y),string):

> Apparently Lua's doesn't allow this because:

Lua does not allow this because it implements true "multiple return
arguments on the stack", wheras Python actually only allows one return
argument, but because it has "tuples", you can pack multiple arguments
into a tuple and return them.

> Personally I would rather have all return values used all the time.
> Potential confusion could be avoided with a little discipline (as
> with other Lua features), and you would really be able to *use* this
> unique feature of Lua.

Personally, I find any semantic where you expand arguments, or where
you have variable numbers of return argument fragile. I much prefer
Python's tuple model for multiple return arguments.

For example, I find this very confusing and thus prone to error:

  function a()
    return 1,2
  end
 
  x = a()    -- x = 1
  x,y = a()  -- x = 1, y = 2

However, since Lua does not have exceptions, it can be useful to use
multiple return arguments to pass back client-optional information
about failures as in:

  function open(filename)
    -- code to open file
    if success then
      return FILE_HANDLE
    else
      return nil, error_code
  end

  f, error_code = open("foobar")
  if not f then
    print("Couldn't open the file because " .. error_code)
    return
  end

I much prefer the exceptions model for this though, such as in Python:

  def open(filename):
    # code to open file
    if success:
      return file
    else:
      raise FileOpenError, (os.error, strerr(os.error))
  
  try:
    f = open("foobar")
  except FileOpenError, reason:
    print "Couldn't open the file because %s" % repr(reason)

-- 
David Jeske (N9LCA) + http://www.chat.net/~jeske/ + [hidden email]