Vararg assignment thread?

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

Vararg assignment thread?

Andrew Starks
I'm curious about why it is not possible to do something like:

`function o.method(self, fun, ...)

success, ... = pcall(fun, ...)
--vararg now return values of fun
-etc.

end
`

I assume this has been a topic, because its use would be pretty obvious. I couldn't come up with anything via the googles, so could someone point the way to where this was discussed?


-Andrew
Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Coroutines
On Fri, Aug 29, 2014 at 9:03 PM, Andrew Starks <[hidden email]> wrote:

> I'm curious about why it is not possible to do something like:
>
> `function o.method(self, fun, ...)
>
> success, ... = pcall(fun, ...)
> --vararg now return values of fun
> -etc.
>
> end
> `
>
> I assume this has been a topic, because its use would be pretty obvious. I
> couldn't come up with anything via the googles, so could someone point the
> way to where this was discussed?

It would be cool to reassign the stored varargs, but I imagine it
wouldn't look too pretty on the stack end of things?  It would be
costly to move the new varargs into place over the old ones of course
-- I'm not sure how varargs are stored on the stack, but I'm thinking
that while it's possible to move the stack frame forward to "forget"
the previous varargs I don't know how you'd point to the new ones
(assuming the lua stack frame has an organized format).

We need more experts!  *rings cowbell*

Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Tim Hill
In reply to this post by Andrew Starks

On Aug 29, 2014, at 9:03 PM, Andrew Starks <[hidden email]> wrote:

> I'm curious about why it is not possible to do something like:
>
> `function o.method(self, fun, ...)
>
> success, ... = pcall(fun, ...)
> --vararg now return values of fun
> -etc.
>
> end
> `
>
> I assume this has been a topic, because its use would be pretty obvious. I couldn't come up with anything via the googles, so could someone point the way to where this was discussed?
>
>
> -Andrew

You can of course use table.pack() to capture vararg return values, which allows you to do what you want indirectly, though it doesn’t answer your question of why you cannot assign to “…” (though I’m not sure that’s such a good idea anyway).

—Tim
Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Philipp Janda
Am 30.08.2014 um 06:38 schröbte Tim Hill:

>
> On Aug 29, 2014, at 9:03 PM, Andrew Starks <[hidden email]> wrote:
>
>> I'm curious about why it is not possible to do something like:
>>
>> `function o.method(self, fun, ...)
>>
>> success, ... = pcall(fun, ...)
>> --vararg now return values of fun
>> -etc.
>>
>> end
>> `
>>
>> I assume this has been a topic, because its use would be pretty obvious. I couldn't come up with anything via the googles, so could someone point the way to where this was discussed?
>>
>>
>> -Andrew
>
> You can of course use table.pack() to capture vararg return values, which allows you to do what you want indirectly, ]...]

I prefer a tail-call to another vararg function in such cases ...

     return handle_pcall( pcall( fun, ... ) )

>
> —Tim
>

Philipp



Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Dirk Laurie-2
In reply to this post by Andrew Starks
2014-08-30 6:03 GMT+02:00 Andrew Starks <[hidden email]>:

> I'm curious about why it is not possible to do something like:
>
> `function o.method(self, fun, ...)
>
> success, ... = pcall(fun, ...)
> --vararg now return values of fun
> -etc.
>
> end
> `
>
> I assume this has been a topic, because its use would be
> pretty obvious.

No, you seem to be the first person who has thought this notion
worth sharing.

I can't see how it can be implemented. `...` is a strand of stack
space belonging to the calling routine and of fixed size. All that
makes it variable is that its size is not known at compile time.

Heck, I don't even see what it can mean. Lua does not have
call-by-name, it only has, for two of its types, mutable values.
What will that assignment to `...` do if the call is
   o:method(fun,1,2,3)?

Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Mike Nelson
In reply to this post by Andrew Starks
On 8/29/2014 9:03 PM, Andrew Starks wrote:

> I'm curious about why it is not possible to do something like:
>
> `function o.method(self, fun, ...)
>
> success, ... = pcall(fun, ...)
> --vararg now return values of fun
> -etc.
>
> end
> `
>
> I assume this has been a topic, because its use would be pretty
> obvious. I couldn't come up with anything via the googles, so could
> someone point the way to where this was discussed?
>
>
> -Andrew

Try http://lua-users.org/wiki/VarargTheSecondClassCitizen for a
discussion of issues. This topic is about Lua 5.1, but nothing has
changed in subsequent versions with regard to varargs except the
addition of table.pack.

Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Dirk Laurie-2
2014-08-30 9:20 GMT+02:00 Mike Nelson <[hidden email]>:

> Try http://lua-users.org/wiki/VarargTheSecondClassCitizen for a discussion
> of issues. This topic is about Lua 5.1, but nothing has changed in
> subsequent versions with regard to varargs except the addition of
> table.pack.

The contents list is:

Issue #1: Vararg Saving
Issue #2: Combining Lists
Issue #3: Selecting the First N Elements in List
Issue #4: Appending One Element to a List
Issue #5: Reversing a List
Issue #6: The map Function
Issue #7: The filter Function
Issue #8: Iterating over Varargs

75% of the space is devoted to eight dubious ways of achieving #1.

The pure Lua solutions to #3 to #7 are for Lua purists. They are
obviously easily solved by writing a little 'tuple' library using the
C API, and I am probably not alone in having done so
as an exercise at an early stage of learning the API.

The main lesson to be gleaned there is that the yearning to make
varargs do things they were not designed for, is older than the date
that most current high-volume contributors to this list started their
activity. I heartily agree with Mike's implication that anyone
who posts or comments on any query/suggestion dealing with
varargs would do well to read that webpage first.

Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Petite Abeille
In reply to this post by Andrew Starks

On Aug 30, 2014, at 6:03 AM, Andrew Starks <[hidden email]> wrote:

> function o.method(self, fun, ...)
>
> success, ... = pcall(fun, ...)
> --vararg now return values of fun
> -etc.
>
> end

So, as Tim has pointed out, table.pack is what you want to make this work.

Something along these lines:

function Try( aFunction, ... )
  local aResult = table.pack( pcall( aFunction, ... ) )
  local ok = aResult[ 1 ]
  if ok then
    return table.unpack( aResult, 2, aResult.n )
  end
end

If you feel adventurous, you could easily build a simple try/catch/finally mechanism out of it.

On the other hand, perhaps this was a purely rhetorical question?!?






Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Andrew Starks


On Saturday, August 30, 2014, Petite Abeille <[hidden email]> wrote:

On Aug 30, 2014, at 6:03 AM, Andrew Starks <<a href="javascript:;" onclick="_e(event, &#39;cvml&#39;, &#39;andrew.starks@trms.com&#39;)">andrew.starks@...> wrote:

> function o.method(self, fun, ...)
>
> success, ... = pcall(fun, ...)
> --vararg now return values of fun
> -etc.
>
> end

So, as Tim has pointed out, table.pack is what you want to make this work.

Something along these lines:

function Try( aFunction, ... )
  local aResult = table.pack( pcall( aFunction, ... ) )
  local ok = aResult[ 1 ]
  if ok then
    return table.unpack( aResult, 2, aResult.n )
  end
end

If you feel adventurous, you could easily build a simple try/catch/finally mechanism out of it.

On the other hand, perhaps this was a purely rhetorical question?!?






Not rhetorical. 

These answers were what I was hoping for.

I use tail calls and unpack, right now. Dirk's answer and the link are gold and explain my question nicely. As for use case:

I've made an "await" function that will await the completion of work from a thread, yielding to a parent thread if it isn't done. It's basically a non-blocking function call. On every check, I call another function that checks the first value (the flag) or if the coroutine is dead.  If completed, it packs the values into a "return_values" table and returns that to the main loop, which unpacks the "return_values" table as the result. 

It works fine. I was just thinking that it would be easier to reassign vararg and skip that function call and the temporary table. 

The set stack space explains why it's not that simple. Thanks!

-Andrew
Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Petite Abeille

On Aug 30, 2014, at 4:16 PM, Andrew Starks <[hidden email]> wrote:

> These answers were what I was hoping for.

Along the same lines, Diego Nehab’s 'Finalized Exceptions’ is a good, pragmatic approach:

http://lua-users.org/wiki/FinalizedExceptions
Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Tim Hill
In reply to this post by Dirk Laurie-2

On Aug 30, 2014, at 1:08 AM, Dirk Laurie <[hidden email]> wrote:
>
> The main lesson to be gleaned there is that the yearning to make
> varargs do things they were not designed for, is older than the date
> that most current high-volume contributors to this list started their
> activity. I heartily agree with Mike's implication that anyone
> who posts or comments on any query/suggestion dealing with
> varargs would do well to read that webpage first.
>

+1 on not changing vararg behavior, I don’t see it making any substantive difference to Lua.

-1 on “read the archives before posting wishes”. Why? Well, though PUC clearly have their own agenda for Lua, isn’t this list at least partly a way to provide feedback on what people would like to see in the language? Part of that must be popularity (note: popular isn’t NECESSARILY good, McDonalds is “popular”). If the procedure was “read archives, if already suggested, do not post” then no idea would ever get more than ONE vote, which distorts the level of interest in things. So yes, before posting search the archives to understand WHY something was not adapted, but then do post if you still feel it is worthwhile imho.

To the OP, as others have noted while it seems an obvious language extension to make “…” read/write, and thus act like a special local variable, the implementation would be expensive since it would involve a bunch of very dubious stack manipulations.

—Tim


Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Jay Carlson

On Aug 30, 2014 12:05 PM, "Tim Hill" <[hidden email]> wrote:

> -1 on “read the archives before posting wishes”. Why?

-1 because of the size and length of archives. If that's the qualification for posting, I'm in trouble; I have 3000 "unread" messages in this folder. I got them the hard way; the easier way is importing mboxes into a mail app (for fast local threaded search).

It might be nice to have a Frequently Offered Clever Suggestions posting. (Everything I needed to know in life I learned from Netrek.)

Jay

Reply | Threaded
Open this post in threaded view
|

Vararg assignment thread?

Andrew Starks


On Saturday, August 30, 2014, Jay Carlson <<a href="javascript:_e(%7B%7D,&#39;cvml&#39;,&#39;nop@nop.com&#39;);" target="_blank">nop@...> wrote:

On Aug 30, 2014 12:05 PM, "Tim Hill" <[hidden email]> wrote:

> -1 on “read the archives before posting wishes”. Why?

-1 because of the size and length of archives. If that's the qualification for posting, I'm in trouble; I have 3000 "unread" messages in this folder. I got them the hard way; the easier way is importing mboxes into a mail app (for fast local threaded search).

It might be nice to have a Frequently Offered Clever Suggestions posting. (Everything I needed to know in life I learned from Netrek.)

Jay

(This is not to jay, but more to the thread, generally)

I make a substantial effort to avoid proposing anything. I did a search, first generally than to the mailing list. I should have done a better job by making another search that targeted the wiki, but I don't think that should be a standard for this kind of question. I think that my question was well within bounds of this list and with netiquette, generally. 

You will notice that:

I did not propose or suggest that vararg be changed. I asked for help finding the thread where this had already been discussed. 

If I do that, then please don't associate me with a "proposal" that I never made. 

It was just a question, and a few of the answers were very helpful (to me). I call that "a good post."

-Andrew
Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Dirk Laurie-2
In reply to this post by Jay Carlson
2014-08-30 18:49 GMT+02:00 Jay Carlson <[hidden email]>:
> On Aug 30, 2014 12:05 PM, "Tim Hill" <[hidden email]> wrote:
>
>> -1 on “read the archives before posting wishes”. Why?
>
> -1 because of the size and length of archives.

Please note that I did not recommend "read the archives".
I merely supported Mike Nelson's advice to read one specific
item, the LuaWiki entry on varargs.

In general, searching the lua-l archives is something
to do on a rainy afternoon when one's plans for outdoor
activity have been scuppered. The LuaWiki, on the other
hand, amply repays an occasional visit. I know at least
one very competent programmer who prefers writing
a LuaWiki article to a long post on the list.

And you have the privilege of editing that article.

Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Ignacio Burgueño-2
In reply to this post by Andrew Starks
Andrew. Somewhat related, I remember Roberto talking about varargs on 2012 workshop.

Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Tim Hill
In reply to this post by Andrew Starks

On Aug 30, 2014, at 10:00 AM, Andrew Starks <[hidden email]> wrote:

> (This is not to jay, but more to the thread, generally)
>
> I make a substantial effort to avoid proposing anything. I did a search, first generally than to the mailing list. I should have done a better job by making another search that targeted the wiki, but I don't think that should be a standard for this kind of question. I think that my question was well within bounds of this list and with netiquette, generally.
>
> You will notice that:
>
> I did not propose or suggest that vararg be changed. I asked for help finding the thread where this had already been discussed.
>
> If I do that, then please don't associate me with a "proposal" that I never made.
>
> It was just a question, and a few of the answers were very helpful (to me). I call that "a good post."
>
> -Andrew

And just to clarify, my point was that *regardless* of prior discussion (archive, Wiki or otherwise), I think people should still post suggestions as it helps everyone understand where others priorities and interests lay.

—Tim


Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Coroutines
On Sat, Aug 30, 2014 at 6:54 PM, Tim Hill <[hidden email]> wrote:

> And just to clarify, my point was that *regardless* of prior discussion (archive, Wiki or otherwise), I think people should still post suggestions as it helps everyone understand where others priorities and interests lay.

I agree -- it's difficult as a member of lua-l to tear down others for
posting about their wants and interests if they do not properly
advertise their passions :D

Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Andrew Starks
In reply to this post by Ignacio Burgueño-2



On Sat, Aug 30, 2014 at 8:13 PM, Ignacio Burgueño <[hidden email]> wrote:
Andrew. Somewhat related, I remember Roberto talking about varargs on 2012 workshop.


Hah. I watched this, and then realized that the bald guy in the front row was me!

The lesson that I learned:

"Keep your fool mouth shut about vararg or they'll take it away!"

:)


--Andrew
Reply | Threaded
Open this post in threaded view
|

Re: Vararg assignment thread?

Coroutines
On Tue, Sep 2, 2014 at 10:06 AM, Andrew Starks <[hidden email]> wrote:

> Hah. I watched this, and then realized that the bald guy in the front row
> was me!

sooo shiney :o  I want to touch it