Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Tech Support (https://forums.graalonline.com/forums/forumdisplay.php?f=7)
-   -   Inconsistencies ( return values ) (https://forums.graalonline.com/forums/showthread.php?t=66822)

JustBreathe 06-22-2006 10:43 AM

Inconsistencies ( return values )
 
It is said that an 'if clause' should return true if the evaluation within isn't false.

PHP Code:

function onCreated()
  {
  
// eval function: return ( var ? true : false );
  // evalFIX function: return ( var != false );

  
eval( new TStaticVar() ); // returns false
  
evalFIX( new TStaticVar() ); // returns true

  
eval( false ); // returns false
  
evalFIXfalse ); // returns false

  
eval( "" ); // returns false
  
evalFIX"" ); // returns false

  
eval( null ); // returns false
  
evalFIXnull ); // returns false

  
eval( "false" ); // returns false
  
evalFIX"false" ); // returns false

  
eval( {"stuff""Things"} ); // returns false
  
evalFIX( {"stuff""Things"} ); // returns true

  
eval( "stuff,Things" ); // returns true
  
evalFIX"stuff,Things" ); // returns true

  
eval( true ); // returns true
  
evalFIXtrue ); // returns true

  
eval( "Stuff" ); // returns true
  
evalFIX"Stuff" ); // returns true

  
}

function eval( var )
  {
  return ( var ? 
true false );
  }

function 
evalFIX( var )
  {
  return (var != 
false);
  } 

As shown:

Objects return false in an if clause... obj != false, returns true.

"string,var" returns true, while {"string","var"} returns false. ( string representation of array, and array )...


I believe it would be better having the evalFix, rather than the eval as return... This would allow flexability as such:

PHP Code:

  if ( pl getPlayer("JustBreathe") )
    {
    
pl.stuff;
    } else {
    return 
false;
    } 


Rick 06-22-2006 12:12 PM

Okay, nevermind, I jumped the gun there by misreading your post... I'll re-edit this post later.



Okay, I've looked at this problem in detail.


evalFIX() works properly, yes.
eval() - the problem may or may not be a problem.


arg ? true : false

arg before it does ?: gets cast to a float -- Objects / arrays get cast to 0.0 obviously, which is why that logic fails with them.

JustBreathe 06-22-2006 02:14 PM

Quote:

Originally Posted by Rick
Okay, nevermind, I jumped the gun there by misreading your post... I'll re-edit this post later.



Okay, I've looked at this problem in detail.


evalFIX() works properly, yes.
eval() - the problem may or may not be a problem.


arg ? true : false

arg before it does ?: gets cast to a float -- Objects / arrays get cast to 0.0 obviously, which is why that logic fails with them.

Mhmm... Replace eval() by...
PHP Code:

if ( var ) return true;
else return 
false

... It does the same as arg ? true: false;

It casts the variable, yes... But that's where the problem is. It should only return false when it is false ("", null, false, "false" ), and in every other occasion, return true ( -1, "foo", {"stuff"}, new Object() ).

Admins 06-22-2006 02:36 PM

I don't know if the behaviour should be changed. The only thing that seems to be different is the check if an object is not null, and return true in that case. You can check that yourself though, with "if (obj==NULL)". For optimization it is currently doing "float(condition)!=false" instead of "condition!=false".

JustBreathe 06-22-2006 02:37 PM

Stefan: Seems like your post was deleted.

( talked about converting to float and bool )

Couldn't you work it out with type() ?
PHP Code:

function eval( var )
  {
  switch( var.
type() )
    {
    case 
0:
      return var != 
false;
    case 
1:
      return var != 
"";
    case 
2:
      return var != 
null;
    case 
3:
      return 
true// ... Just saying it has a type array means it exists!
    
}
  } 


Admins 06-22-2006 02:48 PM

I have deleted and created a new post. It is doing such a check when you compare like 'condition!=false', but for optimization it is doing 'float(condition)!=0'


All times are GMT +2. The time now is 04:47 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.