Reputation: 12611
I have a class like this..
public class Doc {
public function Doc():void {}
public var myVar:Boolean;
}
How can I know if the value held by myVar is default false, or someone has assigned false to it ?!? Isn't there an undefined state? How can I achieve such a thing?
Upvotes: 2
Views: 545
Reputation: 59471
Make myVar a property and use another variable to check if it's been set explicitly.
public class Doc
{
public function Doc():void {}
private var _myVar:Boolean;
private var myVarSetExplicitly:Boolean = false;
public function get myVar():Boolean
{
return _myVar;
}
public function set myVar(value:Boolean):void
{
myVarSetExplicitly = true;
_myVar = value;
}
}
Upvotes: 5
Reputation: 37988
You can't with a boolean, it defaults to false and false === false.
You'd could not strictly type the variable and then use a getter and setter to protect the type
public class Doc {
private var _myVar;
public function set myVar(value:Boolean){
_myVar = value;
}
public function get myVar(){
return _myVar;
}
}
Now, when its not set myVar should === null, and you should only be able to set it to a boolean after that.
But it feels a bit hacky, and I wonder why you need to tell the difference.
Upvotes: 0