mirror of
https://github.com/neovim/neovim.git
synced 2026-05-04 21:15:09 +00:00
fix(vim.ui)!: change open() to return result|nil, errmsg|nil #28612
reverts e0d92b9cc2 #28502
Problem:
`vim.ui.open()` has a `pcall()` like signature, under the assumption
that this is the Lua idiom for returning result-or-error. However, the
`result|nil, errmsg|nil` pattern:
- has precedent in:
- `io.open`
- `vim.uv` (`:help luv-error-handling`)
- has these advantages:
- Can be used with `assert()`:
```
local result, err = assert(foobar())
```
- Allows LuaLS to infer the type of `result`:
```
local result, err = foobar()
if err then
...
elseif result then
...
end
```
Solution:
- Revert to the `result|nil, errmsg|nil` pattern.
- Document the pattern in our guidelines.
This commit is contained in:
@@ -309,6 +309,11 @@ See also |dev-naming|.
|
||||
- return iterable instead of table
|
||||
- mimic the pairs() or ipairs() interface if the function is intended to be
|
||||
used in a "for" loop.
|
||||
- when a result-or-error interface is needed, return `result|nil, errmsg|nil`: >
|
||||
---@return Foo|nil # Result object, or nil if not found.
|
||||
---@return nil|string # Error message on failure, or nil on success.
|
||||
<
|
||||
- Examples: |vim.ui.open()| |io.open()| |luv-error-handling|
|
||||
|
||||
Interface conventions ~
|
||||
|
||||
|
||||
Reference in New Issue
Block a user