I have in a SSJS javascript a function that takes in a parameter. In the function I need to check what type of element the parameter is.
I can check for example if that type is an array:
if(false == values instanceof Array)
I notice that sometimes the incoming parameter is an ArrayList of strings. Is it possible to check against this type also in SSJS?
For now I just convert the object type before I send it to the function.
You can check with
values instanceof java.util.ArrayList
or with
values instanceof java.util.List
if you want to cover all sorts of Java Lists.
Related
I need to check an string attribute length and I don't know what function I have to use, size() maybe?
Context Myclass inv:self.string_attribute->size() <7
-> is for navigating from Collections.
. is for objects/values and an attribute is a value so:
...string_attribute.size()
void main() {
final list = [1].cast<int>();
print('${list.runtimeType}'); // prints CastList<int, int>
}
When I type lis..., Android Studio code completion infers the type as List<int> which it is but when I use runtimeType on it, it prints CastList<int, int>.
Is this IDE fault or Dart?
The cast method is specified to return a List:
List<R> cast <R>()
So this is what your IDE are based on since this is the only guarantee your get. But since other classes can extend/implement List, we are also allowed to return a class which extends from List and return objects based on this new type of List.
E.g. in you example we can see that cast actually returns an instance of CastList. But since CastList implements the interface of List the cast method are allowed to return the CastList instance.
And this is one of the reason why you should never really use runtimeType for anything other than debugging. runtimeType will return the specific type of the object without any details about implemented interfaces.
Instead, you should use the is operator if you want to test for a given type like:
if (obj is List<int>)
The is operator will test if obj implements the interface of e.g. List<int> and not just test if obj is created from this specific class.
This strange behavior baffles me. I intend to create a dictionary with a single array field. Then within this array, two extra sub dictionaries are appended. Here is code,
var dictionary = [String: Any]()
var array = [[String: Any]]()
dictionary["array"] = array
var dict1:[String:Any] = ["abc": 123, "def": true]
var dict2:[String:Any] = ["111": 1.2345, "222": "hello"]
array.append(dict1)
array.append(dict2)
Debugger output.
As you can see from the debugger output, the var array is updated successfully (with 2 sub dictionaries appended). But the dictionary["array"] still has 0 value.
It appears the (dictionary["array"]) and (array) are two separate objects
Yes, they are separate. The element dictionary["array"] is an immutable value of type Array<_> because it's added as a value type to the dictionary not a reference type.
If you tried to add dict1 to the array by addressing the element via it's encapsulating dictionary like this:
(dictionary["array"] as! Array).append(dict1)
You would see an error like this:
error: cannot use mutating member on immutable value of type 'Array<_>'
From the Swift Language docs, emphasis added:
A value type is a type whose value is copied when it is assigned to a variable or constant, or when it is passed to a function.
You’ve actually been using value types extensively throughout the previous chapters. In fact, all of the basic types in Swift—integers, floating-point numbers, Booleans, strings, arrays and dictionaries—are value types, and are implemented as structures behind the scenes.
I am trying to predict the behavior of this code in Groovy
userList.find { }
.find documentation
When the find method is invoked and passed a closure it returns the first element that evaluates the closure into Groovies understanding of true.
When the find method is called without any parameters it returns the first object in the list that matches true according to Groovy truth.
What happens if an empty closure is used?
Will it evaluate to true and thus the first element of the list is returned?
Will it always evaluate to false and after iterating over the list null is returned?
Will it be behave like .find()?
From the Groovy Closures Formal Definition (Alternative Source):
Closures always have a return value. The value may be specified via one or more explicit return statement in the closure body, or as the value of the last executed statement if return is not explicitly specified. If the last executed statement has no value (for example, if the last statement is a call to a void method), then null is returned.
And from Groovy Truth
Object references
Non-null object references are coerced to true.
...
assert !null
That suggests to me that the truth of the return value of an empty closure is always false, so find will never find anything, and thus presumably return null.
there is no definition of "empty closure" only if you delegate this closure to a Map, then the Map keeps empty :
Map closureToMap(Closure c, Map attrs=[:]) {
c.resolveStrategy = Closure.DELEGATE_FIRST
c.delegate = attrs
c()
return attrs
}
def myEmptyClosure = {}
assert closureToMap(myEmptyClosure).isEmpty()
I understand that declaring variables of type Object or passing an Object variable as a parameter in a method is so that we can pass any object type e.g. Integer or String or an array. I just wanted to ask if we can also pass primitive data types or cast to integer primitive types too?
For example if I have a class Stack which allows us to push and pop objects of type Object, then i can use this class for Integer objects BUT can i use it for a primitive type int?
Yes you can, because Java will "auto-box" your primitive type. In other words, if you pass an int to your method, it will first get converted to an Integer, then that Integer will be passed as an argument to your method.
This tutorial gives more details about how it works.