lua-users home
lua-l archive

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


Same argument against it applies, it seems to me

Nil is not a number. It doesn't seem right that %d should treat it
specially, any more than it should if you pass "hello, world" to %d either.

> print (string.format("%d", "hello, world"))
stdin:1: bad argument #2 to `format' (number expected, got string)
stack traceback:
        [C]: in function `format'
        stdin:1: in main chunk
        [C]: ?
> print (string.format("%d", nil))
stdin:1: bad argument #2 to `format' (number expected, got nil)
stack traceback:
        [C]: in function `format'
        stdin:1: in main chunk
        [C]: ?


-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br
[mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of jason zhang
Sent: 24 August 2006 09:26
To: Lua list
Subject: Re: string.format a nil value

Thank you.
How about producing a empty string? 

string.format("Number is %d",data) ==> Number is


----- Original Message ----- 
From: "Andreas Stenius" <kaos@explosive.se>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Thursday, August 24, 2006 4:09 PM
Subject: Re: string.format a nil value


> jason zhang skrev:
> [snip]
>> string.format("%d",novalue), where novalue is a nil variant.
>>  
>> Why not outputing a "NIL" like string? It think it's not very friendly 
>> to print a error information and
>> exit.
> 
> Because code that uses the output may rely on the facy that it should be
> a valid number, since %d was used. If you want "nil" to come out of it,
> i would recommend trying:
> 
> string.format("%s", tostring(novalue))
> 
> Which will work as expected for numbers as well (altough you'll get the
> fraction part as well, which isn't what you'd get with %d)..
> 
> Cheers,
> Andreas
> 
>