GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Error object returned by pcall() declared to always be a string · Issue #79 · TypeScriptToLua/lua-types

Error object returned by pcall() declared to always be a string #79

New issue
Copy link
New issue
Copy link
Open
Open
Error object returned by pcall() declared to always be a string#79
Copy link

Description

@Feuermurmel
Feuermurmel
opened on Nov 6, 2024
Issue body actions

Description

pcall() is declared to return LuaMultiReturn<[true, R] | [false, string]>, but in Lua, the error object can be anything. This leads to problem when the error object is used in a context where it matters whether it can be something other than a string.

To reproduce

Consider the following example (playground):

function foo(this: void) {
    throw {}
}

let result = pcall(foo)

if (!result[0]) {
    print("error: " + result[1])
}

Function foo() throws an empty object. To allow concatenation with a string, a call to tostring() needs to be inserted, but it isn't. So the concatenation in the argument to print() fails:

Lua execution error:
	[string "--[[ Generated with https://github.com/TypeSc..."]:7: attempt to concatenate a table value (field '?')

If the correct type is added to the declaration of result, the call to tostring() is inserted:

let result: [true, void] | [false, unknown] = pcall(foo)

Generated code:

--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]
function foo()
    error({}, 0)
end
result = {pcall(foo)}
if not result[1] then
    print("error: " .. tostring(result[2]))
end

Expected behavior

I think that the return type of pcall() should be changed to LuaMultiReturn<[true, R] | [false, unknown]>. The same applies to coroutine.resume().

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.