question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wrong return type for file:read

See original GitHub issue

Luanalysis v1.2.3

local function file_contains(filename, text)
  local f = assert(io.open(filename, "r"))
  local found = f:read("a"):find(text, 1, true)
  f:close()
  return found
end

What happens?

No such member ‘find’ found on type ‘void’

What were you expecting to happen?

No error message since f:read("a") returns a string.

This is related to https://github.com/Benjamin-Dobell/IntelliJ-Luanalysis/issues/80, where the return type depends on the string value of the first argument as well. Since the type system allows string values as separate types, this one should be solvable with a few overrides though, at least for the most common formats.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
Benjamin-Dobellcommented, Jun 15, 2021

I still need to write tests, but seems as this is being actively discussed. I’m currently looking at typing file:read and io.read as:

5.0-5.1

---@overload fun(): nil | string
---@overload fun(format: "*a"): string
---@overload fun(...: "*n"): (nil | number), (nil | number)...
---@overload fun(...: "*l"): (nil | string), (nil | string)...
---@overload fun(...: number): (nil | string), (nil | string)...
---@vararg "*a" | "*n" | "*l" | number
---@return nil | string | number, (nil | string | number)...

5.2

---@overload fun(): nil | string
---@overload fun(format: "*a"): string
---@overload fun(...: "*n"): (nil | number), (nil | number)...
---@overload fun(...: "*l"): (nil | string), (nil | string)...
---@overload fun(...: "*L"): (nil | string), (nil | string)...
---@overload fun(...: number): (nil | string), (nil | string)...
---@vararg "*a" | "*n" | "*l" | "*L" | number
---@return nil | string | number, (nil | string | number)...

5.3-5.4

---@overload fun(): nil | string
---@overload fun(format: "a" | "*a"): string
---@overload fun(...: "n" | "*n"): (nil | number), (nil | number)...
---@overload fun(...: "l" | "*l"): (nil | string), (nil | string)...
---@overload fun(...: "L" | "*L"): (nil | string), (nil | string)...
---@overload fun(...: number): (nil | string), (nil | string)...
---@vararg "n" | "a" | "l" | "L" | number | "*a" | "*n" | "*l" | "*L"
---@return nil | string | number, (nil | string | number)...

Basically, at present it’s impossible to properly type this function. We’d need some pretty elaborate type transform logic to make it work 100% reliably i.e. the nth output type is dependent on the nth input type.

However, I believe the above should cover off the most common use cases.

0reactions
rilligcommented, Jun 15, 2021

Wow, that already looks impressively accurate, given that the specification of file:read would stretch each and every type system beyond its limits. Well, except for human brains, which apparently are even more flexible at accepting exceptional rules. 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Task has a wrong return type
Task has a wrong return type · No Task in your Button Click event. Just async . It's an event handler. Returns nothing....
Read more >
FileReader: error event - Web APIs | MDN
The error event is fired when the read failed due to an error (for example, because the file was not found or not...
Read more >
fopen - Manual
Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen()...
Read more >
Function FileRead
Return Value ; Success: the binary/string read. @extended is set to the number of bytes/characters returned. ; Failure: sets the @error flag to...
Read more >
WriteFile function (fileapi.h) - Win32 apps
If the function fails, or is completing asynchronously, the return value is zero (FALSE). To get extended error information, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found