lua-users home
lua-l archive

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


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/ + jeske@chat.net