lua-users home
lua-l archive

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


Duncan-

Thanks for pointing out my 0-indexing; I must have Perl on the brain! =P

The reason I wrote the last example as I did is because if I am able to
detect how many return values my function is expected to return, I will
force scalar context for one return value and list context for more than
one.  Here's an example of a function being called in both contexts, but
returning one value:

my $a = foo;
my ( $a ) = foo;

-Rob

On Thu, 29 Apr 2010 18:37:00 +0100
Duncan Cross <duncan.cross@gmail.com> wrote:

> On Wed, Apr 28, 2010 at 5:21 PM, Rob Hoelz <rob@hoelzro.net> wrote:
> > If I were to call foo in Perl, I could do it several ways:
> >
> > my $a = foo; # $a is 3
> > my @values = foo; # $values[0] is 1, $values[1] is 2
> > my ( $a, $b ) = foo; # $a is 1, $b is two
> > my ( $a ) = foo; # $a is 1, the second return value is discarded
> >
> > The equivalent statements in Lua:
> >
> > local a = foo()
> > local values = { foo() }
> > local a, b = foo()
> > local a = ({ foo() })[0] -- not sure how else to do this, really...
> 
> Hi Rob,
> 
> I don't know anything about Perl, but I think I can still usefully
> comment on this last equivalent statement, that you're not sure how
> else to do. All you need to do is:
> 
> local a = foo()
> 
> ...and the second value will be silently discarded. You don't need to
> create a temporary table to do this. (Also, don't forget Lua tables
> index from 1 rather than 0, unless you have patched your version of
> Lua, so ({ foo() })[0] will always evaluate to nil anyway.)
> 
> -Duncan

Attachment: signature.asc
Description: PGP signature