Title: Error object returned by pcall() declared to always be a string · Issue #79 · TypeScriptToLua/lua-types · GitHub
Open Graph Title: Error object returned by pcall() declared to always be a string · Issue #79 · TypeScriptToLua/lua-types
X Title: Error object returned by pcall() declared to always be a string · Issue #79 · TypeScriptToLua/lua-types
Description: 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 some...
Open Graph Description: 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...
X Description: 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 c...
Opengraph URL: https://github.com/TypeScriptToLua/lua-types/issues/79
X: @github
Domain: patch-diff.githubusercontent.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Error object returned by pcall() declared to always be a string","articleBody":"#### Description\r\n\r\n`pcall()` is [declared](https://github.com/TypeScriptToLua/lua-types/blob/master/core/global.d.ts#L180) to return `LuaMultiReturn\u003c[true, R] | [false, string]\u003e`, 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.\r\n\r\n#### To reproduce\r\n\r\nConsider the following example ([playground](https://typescripttolua.github.io/play/#code/GYVwdgxgLglg9mABMOcAUUAWMDOAuRANzhgBMBKRAbwChF7EsAnOAd2oF8auaAbAUyiIm-HCF5CAvIgAOEAIa9eaFHHI0aMYIjQBCEWIkBtAAwBdSrQaymMMFDQAifkxZMCjxAGpho8VCMARgtuGiA)):\r\n\r\n```typescript\r\nfunction foo(this: void) {\r\n throw {}\r\n}\r\n\r\nlet result = pcall(foo)\r\n\r\nif (!result[0]) {\r\n print(\"error: \" + result[1])\r\n}\r\n```\r\n\r\nFunction `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:\r\n\r\n```\r\nLua execution error:\r\n\t[string \"--[[ Generated with https://github.com/TypeSc...\"]:7: attempt to concatenate a table value (field '?')\r\n```\r\n\r\nIf the correct type is added to the declaration of `result`, the call to `tostring()` is inserted:\r\n\r\n```typescript\r\nlet result: [true, void] | [false, unknown] = pcall(foo)\r\n```\r\n\r\nGenerated code:\r\n\r\n```lua\r\n--[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]]\r\nfunction foo()\r\n error({}, 0)\r\nend\r\nresult = {pcall(foo)}\r\nif not result[1] then\r\n print(\"error: \" .. tostring(result[2]))\r\nend\r\n```\r\n\r\n#### Expected behavior\r\n\r\nI think that the return type of `pcall()` should be changed to `LuaMultiReturn\u003c[true, R] | [false, unknown]\u003e`. The same applies to [`coroutine.resume()`](https://github.com/TypeScriptToLua/lua-types/blob/b224030d184a233c8b2eba021b04f07987b631be/core/coroutine.d.ts#L28).","author":{"url":"https://github.com/Feuermurmel","@type":"Person","name":"Feuermurmel"},"datePublished":"2024-11-06T13:17:29.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/79/lua-types/issues/79"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:374de5d4-bfce-d01f-1a14-cc1df014d4ea |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA20:14937D:3C7AD3:50CBF2:6971B5D8 |
| html-safe-nonce | a4a7e49b8f7c7ebffc5c47afd0a073ef0c7950fd00f4177695cb56abfb6a4e45 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTIwOjE0OTM3RDozQzdBRDM6NTBDQkYyOjY5NzFCNUQ4IiwidmlzaXRvcl9pZCI6IjIwNDM4NTM4NTQyMTc0NTA5NjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 13bce4757e011c4894234419aa48463e8932b459eb8a5026ff80c1e45e1367dd |
| hovercard-subject-tag | issue:2638076681 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/TypeScriptToLua/lua-types/79/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3d0f01191d66ba7d16a698df8dd08e539c26b4cc435a414c7dc82a230ca3f2e5/TypeScriptToLua/lua-types/issues/79 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3d0f01191d66ba7d16a698df8dd08e539c26b4cc435a414c7dc82a230ca3f2e5/TypeScriptToLua/lua-types/issues/79 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Feuermurmel |
| hostname | github.com |
| expected-hostname | github.com |
| None | c3b677fea7de290a6c0d1c9ccd6c9909b6a527cf8ee079649e09dc78092d24f1 |
| turbo-cache-control | no-preview |
| go-import | github.com/TypeScriptToLua/lua-types git https://github.com/TypeScriptToLua/lua-types.git |
| octolytics-dimension-user_id | 44121206 |
| octolytics-dimension-user_login | TypeScriptToLua |
| octolytics-dimension-repository_id | 116213653 |
| octolytics-dimension-repository_nwo | TypeScriptToLua/lua-types |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 116213653 |
| octolytics-dimension-repository_network_root_nwo | TypeScriptToLua/lua-types |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 29d7fcca69cafb91ec8a596aaf222fd46e9bc934 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width