[SR-Dev] new operators & if($v) behaviour

Andrei Pelinescu-Onciul andrei at iptel.org
Tue Apr 28 10:33:09 CEST 2009


On Apr 28, 2009 at 09:53, Miklos Tirpak <miklos at iptel.org> wrote:
> On 04/27/2009 04:18 PM, Juha Heinanen wrote:
> >Andrei Pelinescu-Onciul writes:
> >
> > > We could try some sane defaults (for if ($v)).
> >
> >yes, in case there are some.
> >
> > > For example that's what perl does:
> > >             undefined   ""      string  0       other integer
> > > 
> > > $foo eq undef   true    true    false   false   false
> > > $foo == undef   true    true    true    true    false
> > > 
> > > $foo eq ""      true    true    false   false   false
> >
> >if $foo is undefined, it is insane to me that it would be equal to empty
> >string "" or 0.  also comparing two undefined things should not result
> >in true.


Actually if you think in terms of the operators it's not. The operators
forces conversion of its operands to the type it expects.
so for eq (which used for string): $foo eq undef is in fact
 (str) $foo  eq (str) undef , and (str) undef is "".


With undefined you have 2 options: either have valid conversion to
string and ints or bail out with an error. Since we cannot stop the
script or abort(),  even if we report an error we still have to come up 
with a result.

> 
> yes, this is really strange indeed. And ("abcd" == undef) is also true 
> according to the table.

Bear in mind, that == in this case (perl) is an int operator,
so "abcd" == undef  is in fact (int) "abcd" == (int) undef and
(int) "abcd" is 0
(int) undef is 0
=> 0 == 0.

> 
> >
> >what would make sense to me is that comparing an undefined thing to
> >anything that is defined, would result in false.
> 
> I agree.

I don't. We have special operators for checking if something is defined
or not. If you don't use it you want implicit conversion.
In almost all script languages, if a var is undefined then it's equal to
"".


If you think some other behaviour is better, please fill all the table
 and think also about normal operations, e.g. $v int+ $x, $v str+ $x.
Bear also in mind that we support 2 types: int and str. We don't
support bool (bool is int) so all logical expressions are evaluated to
int (so you cannot say bool("a") == true but int("a")==0).
Also think about how other scripting languages handle that.



Andrei



More information about the sr-dev mailing list