this code is written in simple ActionScript, but i'm assuming this problem of mine would occur in all languages that have boolean datatypes.
i'm simply clicking the stage so that my boolean variable reverses its value and than traces/prints/logs it's new value. however, it's always tracing true instead of switching between true and false for each mouse click.
what am i doing wrong?
var myBool:Boolean;
stage.addEventListener(MouseEvent.CLICK, mouseClickHandler);
function mouseClickHandler(evt:MouseEvent):void
{
changeBoolean(myBool);
}
function changeBoolean(boolean:Boolean):void
{
boolean = !boolean;
trace(boolean);
}
You are passing a value to the function, not the reference. This means that boolean value inside your changeBoolean function is copied from myBool variable so when you changed it inside the function, it didn't realy change myBool variable. There are basically two solutions to this:
change the function to not accept parameters and inside it change myBool variable or
change the function so that it returns the boolean parameter and on calling the function, set the myBool valu to the result of the function
In the function changeBoolean, you're changing the value of the boolean (poor name, by the way - try to avoid naming collisions with built-in types, even with different casing) parameter. This has no effect outside that function.
You want to change the value of myBool (which I would call a class field in .Net or Java) instead.
function mouseClickHandler(evt:MouseEvent):void
{
myBool = !myBool;
trace(myBool);
}
...is what I would do (again, with a naive understanding of ActionScript).
Related
For simple data types, you can use e.g.
object is String
to check whether an Object variable is of a more specific type.
But let's you have a List, but want to check if it is a List of Strings. Intuitively we might try
List list = ['string', 'other string'];
print(list is List<String>);
which returns false.
Similarly using the List.cast() method doesn't help, it will always succeed and only throw an error later on when using the list.
We could iterate over the entire list and check the type of each individual entry, but I was hoping there might be a better way.
There is no other way. What you have is a List<Object?>/List<dynamic> (because the type is inferred from the variable type, which is a raw List type which gets instantiated to its bound). The list currently only contains String objects, but nothing prevents you from adding a new Object() to it.
So, the object itself doesn't know that it only contains strings, you have to look at each element to check that.
Or, when you create a list, just declare the variable as List<String> list = ...; or var list = ...;, then the object will be a List<String>.
If you are not the one creating the list, it's back to list.every((e) => e is String).
Each element of a List may be of any type BUT IF YOU ARE SURE that all elements have the same type this approach may be useful
bool listElementIs<T>(List l) {
if (l.isEmpty) return true;
try {
if (l[0] is T) return true; // only try to access to check element
} catch (e) {
return false;
}
return false;
}
void main() {
List list = ['string', 'other string'];
print(listElementIs<String>(list)); // prints 'true'
print(listElementIs<int>(list)); // prints 'false'
}
I think a better way to do this is to be specific about your data types.
By specifying the types of variables, you can catch potential errors early on during the development process.
In addition, specifying the types of variables makes the code more readable and understandable.
Furthermore, specifying types can also improve the performance of your code, as the compiler can make certain optimizations based on the types of variables.
List<String> list = <String>['string', 'other string'];
print(list is List<String>); /// prints true.
You can use this linter rule to enforce it:
https://dart-lang.github.io/linter/lints/always_specify_types.html
I recently found out it was possible to include final in function parameters.
/// Handler for the footer leading checkbox
void _onCheck(final bool value) {
setState(() {
_checked = value;
});
}
However, this feature is not documented anywhere and it's impossible to search any information regarding this topic.
Since the value being passed to the function was already declared elsewhere and could've used var, what are the impacts of using final in function parameters?
It works like declaring any other variable as final - the variable cannot be changed after it has been initialized.
A parameter is really just a local variable where the initializing value comes from the caller instead of a local expression.
So here, you would get an error if you write value = false; in the function because value is a final variable. You would get no error if you removed the final.
Other than that, there is no difference.
In JS, there is undefined and null. Undefined means "no value", null means "value equivalent to emptiness".
In Dart however (and possibly in other languages, I don't know, but right now I'm using Dart), there is no undefined, only null. Therefore it is impossible to make the distinction between a value equivalent to emptiness and the absence of value.
Is there a standard way of simulating this difference in Dart?
No. The null value (of type Null) is the only built-in value that represents the absence of a value. It does not distinguish on why the value is absent.
It's also (almost) the only type you can combine with another type directly in the type system. With null safety, you'll be able to write int? for int-or-Null.
If you want another marker for absence, you can use (or write) an Option type like:
abstract class Option<T> {
final T value;
bool get hasValue => true;
Option(this.value);
factory Option.none() => const _OptionNone();
}
class _OptionNone implements Option<Never> {
const _OptionNone();
bool get hasValue => false;
T get value => throw UnsupportedError("None option has no value");
}
Then you can represent either value or no value.
(There are existing implementations of such a class, for example in the quiver package).
I'm making a function in Delphi that needs a specific value as parameter, unless it is set when function is called. While te default parameter be overwritten in that case?
example:
function ExampleFunction(b = 3, a){
b*a = c
}
ExampleFunction(15,2)
Will the default parameter(3) be replaced with the given parameter(15)?
Your code does not compile. Its syntax is invalid. It looks rather as though you have written the code in some hybrid of Pascal and C#. I suggest that you fix the question.
What's more, default parameters must appear last in the list. The reason for that is that default parameters allow you to omit an parameter when calling the function. When you do that, the compiler substitutes the missing parameter with the default value. Because parameters are positional, it is not possible to omit a parameter, but then pass another parameter that appears after it in the list.
The documentation, which I urge you to read one more time, says:
Parameters with default values must occur at the end of the parameter list. That is, all parameters following the first declared default value must also have default values.
Now to the question. If you do not omit the parameter, that is if you provide it, then the value you provided is used.
Let's use an example that actually compiles:
function Test(a: Integer; b: Integer = 42): Integer;
begin
Result := a * b;
end;
Then
Test(2) = 84 // parameter b is omitted, default value passed
and
Test(4, 3) = 12
I have a Boolean variable in Actionscript 3.
How can I check if it's undefined (not by being false) because false is a value or Boolean in Actionscript is FALSE by default >
If you want a Boolean that can be undefined (essentially a tri-state flag), you can use an Object reference, but just assign the Boolean values true and false to it. Downside is that you lose the type safety.
var isSet:Object = null;
// Later...
isSet = true;
In ActionScript, Boolean can have either true or false value only. If you don't specify any value, it is initialized to false by default.
Edit: This behavior is different from Java's Boolean object type which is a wrapper over primitive boolean. See #Victor's comments below