Log In  

I'm not sure if this is a bug or a deliberate limitation, but it seems that coresume call silently drops everything but the first argument, passed to the yield or return in the coroutine. When you

cor=cocreate(function()
 yield(1,2,3)
 return 4,5,6
end)

s1,a,b,c=coresume(cor)
s2,x,y,z=coresume(cor)

, all of b,c,y,z will be nil. In standard lua passing multiple values out of coroutine is widely used quirk, so it would be nice to have it on PICO-8 too.

P#132005 2023-07-15 15:30

It's intentional (see http://pico8wiki.com/index.php?title=Coresume). PICO-8 uses it's own global functions instead of the Lua coroutine library for coroutine functionality. Possibly more of a feature request than a bug?

P#132007 2023-07-15 16:51
1

I see. Let's call it feature request, yeah. Ability to seamlessly pass around multiple values really simplifies lua code and such arbitrary limitation doesn't seem meaningful.

P#132009 2023-07-15 22:25

Very much agree.

It may have been designed to pass just a single value back, which is sort of a C/C++ way of doing things, where if you can pass at least one thing, it means can pass a pointer or a reference that indirects to as many things as you actually need to pass. In Lua this would mean wrapping your return values with curly braces to turn them into a single table value.

This is what you can do right now to work around the lack of tuple results:

cor=cocreate(function()
 yield{1,2,3}
 return {4,5,6}
end)

function coresume_results(...)
 local s,r=coresume(...)
 return s,unpack(r)
end

s1,a,b,c=coresume_results(cor)
s2,x,y,z=coresume_results(cor)

@zep

It would feel a lot more intuitive just to yield(...) or return ... inside the coroutine and pass the ... tuple back to the caller as the result of the coresume() that ran the routine. I actually thought this was how it worked and I'm surprised it's not symmetrical with how coresume(cor, ...) passes the arg tuple directly.

P#132029 2023-07-16 12:16 ( Edited 2023-07-16 12:19)

Thank you, @Felice - I'll be using your workaround. I would expect that expicit passing of constructed tuples as return value may prevent some codegen optimizations, but, fortunatey, for PICO-8 it's not a hot issue. So, right now it's more matter of style and symmetricity.

P#132033 2023-07-16 13:19

[Please log in to post a comment]