lua-users home
lua-l archive

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


For this reason, I do think that Lua should allows something better for passing variable-length parameter lists in function calls:
   fun(a, b, ...c, d)
   fun(...c, d)
this means that expressions c and d are not isolated and form a single list that is packed to a single one, but that expressions a and b are isolated without needing an extra parentheses.

On the opposite the function call
    fun(a, b) 
would always pass the result of the first _expression_ "a" (even if it's the result of a function call returning a variable list) into the 1st argument passed to fun and
would always pass the result of the first _expression_ "b" (even if it's the result of a function call returning a variable list) into the 2nd argument passed to fun.
(if the _expression_ a was a list, it is implicitly converted to a single value, the first element of that list, or nil if the list is empty; the same on _expression_ b)

Such "casting" of variable lists into single value however will not happen for expressions passed after the explicit "..." operator.




Le lun. 6 sept. 2021 à 19:53, Paul K <paul@zerobrane.com> a écrit :
> The problem is that `('A':byte(2)` actually does not return `nil` in its list of upvalues, it just returns an empty list:

You can wrap it into parentheses to force `nil` if that's what's
needed in this case:

print(type( ( ('A'):byte(2) ) ))

This prints "nil" for me in all Lua versions 5.1+

Paul.

On Mon, Sep 6, 2021 at 9:19 AM Philippe Verdy <verdyp@gmail.com> wrote:
>
> The Lua builtin function "type(value)" unexpectedly signals an error (sometimes but not always) when the argument value is nil.
>
> This occurs when the value nil is the result of a function call, and not a simple variable containing nil or the constant nil. This is incoherent !
>
> That error is wrong. `type(nil)` or `type()` or `type(f())` (where `f()` returns nil or nothing), or `type(nil, 'dummy')` or `type(...)` (called from inside a function called without parameters or with a nil parameter in first position of the varaible argument list) should return 'nil' in all cases.
>
> E.g. run in a Lua console:
>
> val = ('A'):byte(1); typ = type(val); print typ ..'=' ..  tostring(val); print type(('A'):byte(1))
> --> number=65
> --> number
> val = ('A'):byte(2); typ = type(val); print typ .. '=' .. tostring(val); print type(('A'):byte(2))
> --> nil=nil
> --> Error: bad argument #1 to 'type' (value expected).
>
> The problem is that `('A':byte(2)` actually does not return `nil` in its list of upvalues, it just returns an empty list:
> * when assigning an empty list to a variable, that variables get `nil` (with the correct type).
> * when passing the empty list returned by a function to `type()`, this function gets no parameter and incorrectly signals an "error".
>
> Yes the builtin `type(value)` function "requires" at least one argument, but that argument in first position CAN be `nil`. So it may not be passed at all and the first argument should be initialized with the `nil` value, and the builtin `type(value)` function SHOULD return the string `"nil"`, without any error (which I think is a very legacy behavior of very old versions of Lua before major changes in how lists of values were passed and returned through function calls).
>
> And the builtin `string.byte(index)` function SHOULD also be fixed to return a true `nil` for a given index past the end of string, and not an empty list (or this should not make any difference for calling type()).
>
>