lua-users home
lua-l archive

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


> >
> > > Okay, so I'm obviously missing something with this.  Can anyone 
> > > explain the behaviour from this:
> > >
> > > function test1()
> > >     return 1, 2
> > > end
> > >
> > > function test2()
> > >     return 3,4
> > > end
> > >
> > > print(test1(), test2())
> > >
> > >> 1    3    4
> > >
> > > I would have expected it to output 1    2    3    4.  Why 
> doesn't it?
> >
> > Only the last function in a comma list has multiple return values 
> > respected.
> 
> On 9/12/07, Tom Miles <Tom@creative-assembly.co.uk> wrote:
> > Is there a good reason for that?  I assume there is, but 
> I'ld like to 
> > know, as it seems a bit counter-intuitive.
> 
> At first sight, it's counter-intuitive.
> Think about how you'd write the print() function so that it 
> took multiple variadic arguments(??).
> 
> in your example "print(test1(), test2())", if the output was 
> "1 2 3 4", from the standpoint of print(), what part of the 
> output is the first argument, what part is the rest? How 
> would you write such a function in Lua?
> 

I'm not sure I understand your point.  In my mind, if I have two
functions returning 2 values, then I have 4 values plain and simple.  It
would be treated exactly the same as print(1,2,3,4).  Is there a reason
not to do it that way? 

> -- count & print the arguments one per line function myprint(...)
>    for i,v in pairs({...}) do
>       io.write(i .. ":  " .. v .. "\n")
>    end
> end
> 
> myprint(test1(), test2())
> 1:  1
> 2:  3
> 3:  4
> 
> So, I guess you're hoping the expansion of "print(test1(), test2())"
> would be "print(1,2, 3,4)" but that would be rather difficult 
> to handle for anyone writing variadic functions - consider
> 
> 
> function variadic(a, b, ...)
>    print(a, b, ...)
> end
> variadic(test1(), test2())
> 
> What should be in a? what should be in b?
>

Again, I don't see your point!  What should be in a and b, or what 'is'?
Obviously, I think that a = 1, b = 2, and ... Contains 3, and 4, but
that's not what is. :/

As I said before, there is obviously a good reason for the way it is,
greater minds than mine have formulated the language, but I don't think
you have provided me with a convincing argument.  Thanks for the
feedback though.
 
> -K
> 
> PS. Top-posting and HTML-encoded messages are generally 
> accepted as no-noes.
> 

Sorry about that.  I hope I've fixed it, but if there is a massive html
sig attached to this then I have no control over that as all outgoing
email has it added automatically by our companys mail server.
 








DISCLAIMER 
This email is sent by The Creative Assembly Limited company No. 03425917, registered in England &Wales registered office 27 Great West Road, Middlesex, TW8 9BW, England. The contents of this e-mail and any attachments are confidential to the intended recipient and may also be legally privileged. Unless you are the named addressee (or authorised to receive for the addressee) of this email you may not copy, disclose or distribute it to anyone else. If you have received this email in error, please notify us immediately by e-mail on postmaster@creative-assembly.co.uk and then delete the email and any copies. The Creative Assembly Limited have made all reasonable efforts to ensure that this e-mail and any attached documents or software are free from software viruses, but it is the recipient's responsibility to confirm this.