lua-users home
lua-l archive

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


Another possibly would be to simply use double commas ",," between two expressions give in a parameter list (or in a table constructor) to allow them to be merged into a single list whose all members would be then enumerated. Using a single comma "," between two expressions ensures that they are isolated and if they are variable-sized lists, they are downcasted to a singleton value (or nil).

So:
  fun(a,b) is isolating "a" from "b", but "b" may still be variable-sized list as it is in last position before the closing parenthesis
  fun(a,,b,c) is merging lists "a" and "b" into a single rebindable list, but "c" is still isolated and remains the 3rd parameter, so that the list "a,,b" is limited to contain at most 2 values
  fun(a,b,,c)  is merging lists "b" and "c" into a single rebindable list (for arguments 2 and higher), but "a" is still isolated and downcasted to a single value or nil
  fun(a,b,c,) is isolating and downcasting every one of the 3 parameters, each one passed as a single value or nil (the trailing comma after c is important here)







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()).
>
>