lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


(written without access to a Lua interpreter, bugs unlikely but possible)

  local function xpack( x, ... )
    return x, table.pack( ... )
  end
  function suppress( f, ... )
    local _, rets = xpack( pcall( f, ... ) )
    return table.unpack( rets, 1, rets.n )
  end

-- nobody

On 27/10/2020 20.25, Grzegorz Krasoń wrote:
Function that returns nothing seems different than function that returns `nil`:

```lua
function f()
     return nil
end

function g()
end

print(f()) -- prints nil
print(g()) -- prints empty line
```

How to recognize this at the function call? For example how to implement an universal wrapper that would suppress all errors and still returns exactly the same thing as wrapped function in case of success?

```lua
function supperess(f, ...)
     success, result = pcall(f, ...)
     return result
end

print(supperess(f)) -- prints nil
print(supperess(g)) -- also prints nil, how to fix it?
```