PDA

View Full Version : Booleans - True, False, or Default



IQAndreas
January 28th, 2010, 08:27 AM
I want a Boolean to have one one of three properties; true, false, or default.

Otherwise I need code like this in the getter/setter:

//WHAT I'M USING
private var _useTimestamp:Boolean;
private var _useTimestampDefault:Boolean;

public function get useTimestamp():Boolean
{
if (_useTimeStampDefault)
{ return GlobalSettings.useTimestamp; }
else
{ return _useTimestamp; }
}
public function set useTimestamp(value:Boolean)
{
_useTimestampDefault = false;
_useTimestamp = value;
}

public function get useTimestampDefault():Boolean
{
return _useTimestampDefault;
}
public function set useTimestampDefault(value:Boolean)
{
_useTimestampDefault = value;
}

This is what I want to do instead:

//What I want, A LOT less code
private var _useTimestamp:Boolean;

public function get useTimestamp():Boolean
{
if (_useTimestamp == default)
{ return GlobalSettings.useTimestamp; }
else
{ return _useTimestamp; }
}
public function set useTimestamp(value:Boolean)
{
//Timestamp can be set to "true, false, or default/null"
_useTimestamp = value;
}


Also, I want to use this feature as default values in properties

public function traceMe(str:String, useTimestamp:Boolean = GlobalSettings.useTimestamp)
{ ... }
But that throws the following error:
1047: Parameter initializer unknown or is not a compile-time constant.


I could create a new class that has one single property which handles boolean values, but that means that it is treated as an instance, and not as a real value, which means that statements like this might be off:
if (customBool) { ... }


Is this possible? If I set the boolean to null, it will default to false, and that is not what I want.

gamera
January 28th, 2010, 09:13 AM
use a string

IQAndreas
January 28th, 2010, 09:26 AM
use a string
Sadly, they take up a lot more memory than booleans (at least 8 times more), and they won't always be evaluated properly, or at least not quickly when checking if they are true/false. But, good idea, and it works, just not well enough.


I could also do what I do with default values for integers, and have -1 serve as a default marker, while 0 is false and 1 is true, but again, it wastes a lot more space, and if the users see an integer value as a parameter, unless they have read the documentation or know ahead of time, they may not realize they are only allowed to input a 0 or a 1, or maybe a -1.

Yet, the second option is likely the best course so far...


Hm... Maybe if I make constants, TRUE (1), FALSE (0), DEFAULT(-1). Then users don't need to deal with numbers, and can write like this:
traceMe("Hello world", DEFAULT);

Hm... Yes, that is an option...

Jeff Wheeler
January 28th, 2010, 11:27 AM
It's common in Java to use the Boolean datatype, which can be null, true, or false. Something similar might be possible in ActionScript.

annielhy
January 28th, 2010, 11:54 AM
How about untyped?

Object data type

The Object data type is defined by the Object class. The Object class serves as the base class for all class definitions in ActionScript. The ActionScript 3.0 version of the Object data type differs from that of previous versions in three ways. First, the Object data type is no longer the default data type assigned to variables with no type annotation. Second, the Object data type no longer includes the value undefined, which used to be the default value of Object instances. Third, in ActionScript 3.0, the default value for instances of the Object class is null.
In previous versions of ActionScript, a variable with no type annotation was automatically assigned the Object data type. This is no longer true in ActionScript 3.0, which now includes the idea of a truly untyped variable. Variables with no type annotation are now considered untyped. If you prefer to make it clear to readers of your code that your intention is to leave a variable untyped, you can use the new asterisk (*) symbol for the type annotation, which is equivalent to omitting a type annotation. The following example shows two equivalent statements, both of which declare an untyped variable x:
var x
var x:*

Only untyped variables can hold the value undefined. If you attempt to assign the value undefined to a variable that has a data type, Flash Player will convert the value undefined to the default value of that data type. For instances of the Object data type, the default value is null, which means that Flash Player will convert the value undefined to null if you attempt to assign undefined to an Object instance.

Source: http://www.adobe.com/livedocs/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000047.html

I use this method a lot in AS2.

_kp
January 28th, 2010, 12:07 PM
Also, I want to use this feature as default values in properties

public function traceMe(str:String, useTimestamp:Boolean = GlobalSettings.useTimestamp)
{ ... }
But that throws the following error:
1047: Parameter initializer unknown or is not a compile-time constant.


This works if you make it compile time constant:

private static const defaultTimestamp:Boolean = GlobalSettings.useTimestamp;
public function traceMe(str:String, useTimestamp:Boolean = defaultTimestamp)
{ ... }
It is of course constant now and can't be changed, which may not be what you want.