[SR-Users] how can string "0" be equal to int 0?

Iñaki Baz Castillo ibc at aliax.net
Fri Oct 15 18:02:41 CEST 2010


2010/10/15 Daniel-Constantin Mierla <miconda at gmail.com>:
> Even we like it or not, non-type-safety languages such as perl, shell or
> python rule the sys admin world.

Said that I would like to expose a proposal for comparisons:


1) Never do implicit type conversion. This is, a string is NEVER equal
to an integer:

    if "0" == 0     =>  false
    if "" == 0       => false
    if "asdasd" == 0     => false


2) An integer is NEVER equal to a string:

    if 0 == "0"     =>  false
    if 0 == ""       => false
    if 0 == "asdasd"     => false


3) Integer 0 is not equal to NULL:

     if 0 == NULL   => false


4) Empty string is not equal to NULL:

     if "" == NULL  => false


5) Integer 0 is true:

     if 0   => true


6) Empty string is true:

     if ""  => true



The only point I'm not sure is the last one (6). In some high level
languages (as Ruby) checking an empty string returns true. However in
other languages (as ActionScript) it returns false.


-- 
Iñaki Baz Castillo
<ibc at aliax.net>



More information about the sr-users mailing list