lua-users home
lua-l archive

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


On Tue, Sep 30, 2014 at 12:48 PM, Thiago L. <fakedme@gmail.com> wrote:
>
> On 30/09/14 04:37 PM, Coda Highland wrote:
>>
>> On Tue, Sep 30, 2014 at 12:21 PM, Tom N Harris <telliamed@whoopdedo.org>
>> wrote:
>>>
>>> On Wednesday, September 24, 2014 10:07:29 PM Andrew Starks wrote:
>>>>
>>>> Does this mean that we're going to loose coroutines? :)
>>>
>>> If by "loose" you mean "set free", then yes perhaps it will.
>>>
>>> (My apologies. It was too good to pass up.)
>>>
>>> But instead of seeing goto as a continuation, what if it's a tail-call?
>>>
>>>      function A()
>>>        local N=0
>>>        local C=function()
>>>          N=N+1
>>>          goto B
>>>        end
>>>        N=1+C()
>>>        ::B::
>>>        return N
>>>      end
>>>      print(A())
>>>
>>> Does this print 1 or 2? Assuming a hypothetical Lua that allows such a
>>> thing.
>>>
>> It prints 1. You've skipped over the assignment instruction.
>>
>> /s/ Adam
>>
> If I got this right, the goto B is running A's code inside C... so it prints
> 2
>

No, the goto B is skipping over the normal control flow to continue
execution at the label. Think of it more like an exception than a
coroutine:

function A()
  local N=0
  local C=function()
    N=N+1
    error("goto B")
  end
  pcall(function() N=1+C() end)
  -- ::B:: goes here
  return N
end
print(A())

/s/ Adam