put label after return?

57 views
Skip to first unread message

Rett Berg

unread,
Nov 30, 2025, 9:30:53 AM (3 days ago) Nov 30
to lu...@googlegroups.com
This doesn't appear to parse:

function showSkip()
  for i=1,10 do
    if i%2 == 0 then goto skip end
    return i
    ::skip::
  end
end

> label_after_return.lua:7: 'end' expected (to close 'for' at line 3) near '::'

In order for it to parse I need to use something like

  ...
  if true return i end
  ::skip::

Is there an alternative way to get this to parse without requiring a branch or similar?

Note: I don't mean fix the function, I am aware I could use an "else" instead or otherwise change the if block. I give the above example as demonstration only.

- Rett

Rett Berg

unread,
Nov 30, 2025, 9:32:53 AM (3 days ago) Nov 30
to lu...@googlegroups.com
This is the actual code if anyone is interested. Basically I use the goto to get out of an inner loop that needs to be skipped.

--- use as an iterator.
Iter.__call = function(it)
  local li, k = it._li
  for key, v in it[1], it[2], it._nextK do
    k, it._nextK = key, key
    for i=-1,li,-1 do
      k, v = it[i](k, v); if k == nil then goto skip end
    end
    if true then return k, v end -- `if` is necessary for parser
    ::skip::
  end
end

Sainan

unread,
Nov 30, 2025, 9:35:22 AM (3 days ago) Nov 30
to lu...@googlegroups.com
The keyword you are looking for is 'break'.

-- Sainan

Martin Eden

unread,
Nov 30, 2025, 10:20:59 AM (3 days ago) Nov 30
to lu...@googlegroups.com
On 2025-11-30 16:30, Rett Berg wrote:
> This doesn't appear to parse:
>
> function showSkip()
> for i=1,10 do
> if i%2 == 0 then goto skip end
> return i
> ::skip::
> end
> end

"For syntactical reasons, labels in Lua are considered statements" [3.3.4]
"The return statement can only be written as the last statement of a
block." [3.3.4]

-- Martin

Christophe Delord

unread,
Nov 30, 2025, 1:27:37 PM (3 days ago) Nov 30
to lu...@googlegroups.com

The branch can be avoided with a simple do…end block:


do return k, v end

::skip::
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CALW7ahzwb7Zcye4WSzQ%3DgRoCS_0MKPcjB43HQQ3sjB5dRq_qFA%40mail.gmail.com.

Rett Berg

unread,
Nov 30, 2025, 1:46:27 PM (3 days ago) Nov 30
to lu...@googlegroups.com
Perfect, I didn't even think of that!

You received this message because you are subscribed to a topic in the Google Groups "lua-l" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lua-l/W3egpuKW9dY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to lua-l+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/lua-l/74c3c4a5-5adc-4147-80e7-8282a60d4a40%40cdelord.fr.

Gé Weijers

unread,
Dec 1, 2025, 2:24:33 AM (2 days ago) Dec 1
to lu...@googlegroups.com

On Sun, Nov 30, 2025 at 6:32 AM Rett Berg <goog...@gmail.com> wrote:
[...] 
  ...
  if true return i end
  ::skip::

Is there an alternative way to get this to parse without requiring a branch or similar?


The usual idiom is

do
  return
end
::skip::
 
The compiler recognizes "if true" so your "if true then return end" produces the same code.

--

Reply all
Reply to author
Forward
0 new messages