aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/F.lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/vim/F.lua')
-rw-r--r--runtime/lua/vim/F.lua32
1 files changed, 22 insertions, 10 deletions
diff --git a/runtime/lua/vim/F.lua b/runtime/lua/vim/F.lua
index 3e370c0a84..5ed60ca8ab 100644
--- a/runtime/lua/vim/F.lua
+++ b/runtime/lua/vim/F.lua
@@ -1,18 +1,30 @@
local F = {}
---- Returns {a} if it is not nil, otherwise returns {b}.
+--- Returns the first argument which is not nil.
---
----@generic A
----@generic B
+--- If all arguments are nil, returns nil.
---
----@param a A
----@param b B
----@return A | B
-function F.if_nil(a, b)
- if a == nil then
- return b
+--- Examples:
+---
+--- ```lua
+--- local a = nil
+--- local b = nil
+--- local c = 42
+--- local d = true
+--- assert(vim.F.if_nil(a, b, c, d) == 42)
+--- ```
+---
+---@param ... any
+---@return any
+function F.if_nil(...)
+ local nargs = select('#', ...)
+ for i = 1, nargs do
+ local v = select(i, ...)
+ if v ~= nil then
+ return v
+ end
end
- return a
+ return nil
end
-- Use in combination with pcall