Please login or register.

Login with username, password and session length
Advanced search  

News:

Latest WME version: WME 1.9.1 (January 1st, 2010) - download

Author Topic: boolean evaluation inconsistency  (Read 3264 times)

0 Members and 1 Guest are viewing this topic.

jbw

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 24
    • View Profile
boolean evaluation inconsistency
« on: April 02, 2008, 11:20:37 AM »

Code: [Select]
booltest(1);
booltest("1");
booltest(new String("1"));

function booltest(b)
{
  var q; if (b) q = "true"; else q = "false";
  Game.LOG("WME treats [" + b + "] as " + q);
}
Result:
  WME treats [1] as true
  WME treats [1] as true
  WME treats [1] as false
Logged

metamorphium

  • Global Moderator
  • Addicted to WME forum
  • *
  • Karma: 12
  • Offline Offline
  • Gender: Male
  • Posts: 1511
  • Vampires!
    • View Profile
    • CBE  software s.r.o.
Re: boolean evaluation inconsistency
« Reply #1 on: April 02, 2008, 03:40:53 PM »

I am not sure this is a right thing to do.

how about:

Code: [Select]
var t = new String("1");

booltest(t);

?
Logged
J.U.L.I.A. Enhanced Edition, Vampires!, J.U.L.I.A., J.U.L.I.A. Untold, Ghost in the Sheet

jbw

  • Lurker
  • *
  • Karma: 0
  • Offline Offline
  • Gender: Male
  • Posts: 24
    • View Profile
Re: boolean evaluation inconsistency
« Reply #2 on: April 02, 2008, 10:44:07 PM »

I am not sure this is a right thing to do.

I am pretty sure it is. As you can see, my test routine displays what it get as argument and it is "1". This code is not just from my head, but from the working example.
I've done a bunch of tests to catch the truth about booleans. It doesn't matter if you pass variable referencing string object or immediately created object in the call itself, result is always the same.

I have experience of creating compilers, so I know this is the hell of implicit conversions. It's hard to track in mind all weird possibilities. In this case perhaps no string should match "true" (the easiest way), but if the one ordinary string of digits does, obviously the corresponding string object should match it as well.
Logged
 

Page created in 2.644 seconds with 22 queries.