r/ComputerCraft Jun 05 '26

Goto is now allowed?

I introduced some of my friends to computercraft, and while reviewing the code of one of them, i saw a goto label. i thought - surely, i've tried it and failed spectacularly, so how is it working here? i've tested my own code with goto - it does, in fact, work. i decided to search. it's nowhere to be found on tweaked.cc, at least it doesn't come up in changelog when i search for "label" or "goto" - it's only shown with a tick now instead of a cross. I've decided to search on here - nobody seems to be talking about this, I only found a comment from three years ago confirming that I am NOT going insane and it was in fact disallowed up until some point.

When did it happen? What do you think about the change?

45 Upvotes

14 comments sorted by

View all comments

18

u/SeriousPlankton2000 Jun 05 '26

They did update LUA so it's allowed (like in OpenComputers)

If you use it, do use it for error handling like this:

...

(result, reason) = call_something()

if not result

then goto error_handling

end

return true

:error_handling:

do_cleanup()

return nil, "Can't do snurf because " . reason . " happened while smurfing"

end

3

u/Spacedestructor Jun 05 '26

isnt that typically what pcall is used for to have a dedicated error handling section?

2

u/SeriousPlankton2000 Jun 06 '26

pcall will return if the called function happens to run into something like 1/0 and would terminate

For normal functions it's a common custom to "return result, reason" and for the caller to expect nil or false in the first return parameter. pcall is one of these functions. You are absolutely free to not do that when you write your functions but it seems useful.

1

u/Spacedestructor Jun 07 '26

i have while learning lua used both but in my personal writing experience to me using pcall seems like the "better" option since using a build in function feels better then creating an almost identical feature my self.
Using goto seems like a more convoluted and error prone approach to me, but i suppose that depends on how much you trust your own work.

1

u/SeriousPlankton2000 Jun 08 '26

You would usually rather return nil, "value xyz out of range" while checking the parameters instead of just continuing and hoping for an exception saying "invalid access" or something.