Swift Array Declaration Difference - swift-array

What is the difference between below declaration of array syntax in swift?
var arr:[Int]
var arr=Array<Int>()
and which one is better?
How and Why?

var arr:[int] this is fixed size array and don't change size after initialization.
var arr=Array() this is array list and this array change the size with respect to number of elements. you can easily remove and add element easily in this array.

var arr:[Int]
This one simply declares an array of Integers named arr. It does not initialise the array and hence is not usable.
var arr=Array()
This one declares as well as initialises the array. We can add whatever we want to this arr.
The second one is better since its initialised and usable.

Related

ProtoBuf - Can a field support two possible datatypes?

is it possible for one field in protobuf to support/accept a string or an array of string.
This is my message:
message MessageA {
string fieldId =1;
string method = 2;
google.protobuf.Any value =3;
}
The reason for it being dynamic is because an array of string or a simple string can be the input. There are separate methods on what will happen if the payload is string[] or string.
Right now, I'm using any but I'm not sure what to do in the any file. I've read that oneof does not support array data types thats why I'm trying to make it work with any.
Here is the error message when I try to put a repeated inside a oneof:
No, basically. The main way of doing this would be to have two inner-types - one with a single-valued member, one with a repeated member, and have a oneof that covers the two inner-types. Or perhaps simpler: just have a repeated and separately have an enum, boolean, or other indicator to choose between the two possible meanings (so you can tell between "an array, that happened to have exactly one item" vs "the single value").
For anyone that might have the same problem as me, you can use google.protobuf.Value. There would be an additional object but it can be transformed using UsePipe so it shouldn't be a problem.
Here is the documentation: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf#google.protobuf.Value

How ejs Extract values from object

I am using Sequelize where I fetch all the data from a table using findAll. Which is basically an Array of objects. What seems to be confusing is that the data I am showing as output nested under objects. (Sounds Confusing? Let me clarify)
So, Let's I have this Short Code
Here if I run this code, it will give me undefine because father lies in parent, for which I have to use user.parent.father, Right?
Okay, now on Fetching data from table in my code,
I console.log my first row, for which I get this.
Now here the values which I need lies in dataValues.
In my ejs file. I am using simple for-of loop
Now my Question is why am I not getting undefined for product.title , product.imageUrl and so on? It is supposed to get those data by product.dataValues.title. Because It lies in another object names dataValues.
Technically, when a value is initialized by Sequelize, your object's prototype is set to Model (the class is too long to copy-paste it here).
When you create your model, Sequelize calls init on it (line 424) and which in turn calls refreshAttributes.
This one calls Object.defineProperty to define both the getter and the setter for each property you have defined in the metadata (line 1238).
The getter and setter are set to get and set functions respectively (lines 1095 to 1103).
This actually means that
instance.field
is just a property wrapped over
instance.get('field')
This corresponds with their docs which says
Instance instances operate with the concept of a dataValues property, which stores the actual values represented by the instance. By default, the values from dataValues can also be accessed directly from the Instance, that is:
instance.field
is the same as
instance.get('field')
is the same as
instance.getDataValue('field')

How to use BigDecimal in Xpages?

I'm using stored numeric values in calculations and matching situations and javascript doubles are a big "NO-NO" when doing these kind of operations.
However I can't find a solution on how to use java BigDecimal in SSJS in Xpages.
Since one should construct a BigDecimal using a string I have tried different approaches i SSJS. Whatever test the result is the same, the call is ambiguous:
Ambiguity when calling new java.math.BigDecimal(long) and new
java.math.BigDecimal(int)
How do I use a BigDecimal in my SSJS when values are stored in documents as Numbers?
How do I use BigDecimal with a string argument when values are stored in documents as Numbers?
edit/amend:
After accepting Svens answer I got a bit further and to my second question.
The value retrieved from the document is 451368 but it will be stored in variable as 451367.99999999994
How do I recover from that when the user should match against original value?
Use Java-Objects instead:
var value = new java.lang.Integer(1);
new java.math.BigDecimal(value);

how to initialize PriorityQueue

I'm reading the API http://docs.oracle.com/javase/6/docs/api/java/util/PriorityQueue.html. I'm still a bit lost.
Which is the right way to initialize?
PriorityQueue(Caller caller_pq);
or
static PriorityQueue<Caller> caller_pq;
caller_pq = new PriorityQueue<Caller>();
If caller_pq is the variable you want to hold your PriorityQueue then the second one --
static PriorityQueue<Caller> caller_pq;
caller_pq = new PriorityQueue<Caller>();
-- is correct.
It's hard to tell what you mean by your first option PriorityQueue(Caller caller_pq). If you're intending to create a PriorityQueue containing elements of type Caller (which is what I think you're intending), then no, that won't work.
If Caller implements Collection or extends PriorityQueue then it will make a PriorityQueue containing the elements in caller_pq (as long as you precede it with new).

Naming Vars with strings

Can a variable be named with a string or character array, in any language? Basically I want something like:
Var_String = "varname"
Var_String as double
And then I could fill the double varname.
If it helps im trying to make a program that can declare variables on the fly, while running. Even if thats not possible, I am open to workarounds even if they're impractical, although I would prefer that workarounds be in VB6, C++, or PHP, because I know those languages already, but they dont have to be.
Javascript is completely capable of declaring variable names on the fly. A javascript object can be treated "associatively" as a dictionary. Observe:
var testyObject = function()
{
Awesome = "hello";
};
var myObject = new testyObject();
alert(myObject.Awesome); // creates an alert window that says hello
alert(myObject['Awesome']); // the same as above
myObject[myObject.Awesome] = "woo!"; // We just created a property on the object with the name "hello"
alert(myObject.hello); // creates an alert window that says "woo!"
I also believe you can add them to your immediate scope rather than as properties on other objects by using this["whatever you want it named"] = "woo!"; but I'm not certain, someone can correct me on that if such does not work.
You can read more about associative arrays at http://www.quirksmode.org/js/associative.html
The usual way to do something like this is called a hash. You store name/value pairs and given the name, can look up its value. You can generally define them to store any sort of object. In fact, in some languages, objects themselves are essentially hashes with a few extra properties.
You can find more information on wikipedia: http://en.wikipedia.org/wiki/Hash_table

Resources