[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: weird behaviour of table.sort
- From: HyperHacker <hyperhacker@...>
- Date: Mon, 28 Jun 2010 13:55:13 -0600
On Mon, Jun 28, 2010 at 12:33, Wolfgang Pupp <wolfgang.pupp@gmail.com> wrote:
> This code:
>
> local function lesser(a, b)
> print(a,b)
> return true
> end
> table.sort({1,2,3,4}, lesser)
>
> gives:
> 4 1
> 2 4
> 3 4
> 4 4
> 1 4
> nil 4
> and crashes with "invalid order function for sorting". That means that
> all necessary checks are already in place; I'm just asking for not
> making that last comparison with the nil argument, since it serves no
> purpose (calling the comparison function with an argument which is not
> to be sorted, yields no additional information about the validity of
> the comparison function) and may obfuscate the true source of the
> error (e.g. "attempt to index a nil value", instead of "invalid order
> function for sorting").
>
If it's calling your order function with nil as an argument, and
throwing the "invalid order function" error only after that, then it
seems like it simply hasn't figured out that your function is invalid
at that point, so it can't reasonably be expected to say so. Though I
do wonder if either argument could ever be nil given a valid order
function.
--
Sent from my toaster.