lua-users home
lua-l archive

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


-----Original Message-----
From: Francisco Olarte

On Sun, Jan 28, 2018 at 1:09 PM, Tony Papadimitriou <tonyp@acm.org> wrote:
...
The only thing I would certainly add to Lua is a `yield` keyword to
complement `return` similar to Python’s way.

The current method (or hack) of implementing co-routines is unnecessarily
verbose and often resulting in hard to understand code.

How do you think this will improve?

Re-read the sentence just above your question to see how.

In Lua? Coroutines are what they have been in nearly every other
system I've used, same functions as packages I've used in C, C++,
Pascal and even assembler ( 8 bit one ). Nothing especial to be done
in any function used in a coroutine heavy program, just a couple
function calls.

An what more 'special' is done with Python's `yield`?  You just
replace `return` with `yield` and you turn any function into a
co-routine.  Can't get much simpler.

I didn't talk about changing how co-routines are implemented internally,
(and, as a user, it's none of my business), just how one has to implement
them in their own Lua code.  Call it 'syntactic sugar' if you like.
Same as having `a = function() end` be (almost) equivalent to
`function a() end`

In other words, I prefer this:

function xxx(n)
 for i = 1, n do yield i*i end
end

to this:

function xxx(n)
return coroutine.wrap(function() for i = 1, n do coroutine.yield(i*i) end end)
end

Now, if the latter is more appealing to you, good for you.
"In matters of taste, there can be no disputes."

Francisco Olarte.

Tony P.