I am not able to clearly understand what is typed array in kotlin. I have seen the fucntion toTypedArray in kotlin. but did not see any proper definition of it like what exactly it does. Can anyone please explain with an example.
Thanks
Arrays are generic data structures because they can contain different types of elements. You can have Array<Int> or Array<String> for instance.
There is no separate concept of "typed" array. The reason for the name of toTypedArray is (I guess) to distiguish it from toArray() which returns an Array<Any?> (without useful type information about its elements, because everything is a Any? in Kotlin).
The reason why those 2 exist is because arrays on the JVM cannot be created without knowing the element type. This means that, in general, you cannot create an arbitrary array generically because generics are erased at runtime so you wouldn't actually know the correct element type at that time. This is why the simple toArray method either returns Array<Any?> or takes an extra array argument. The extra argument allows to either avoid creating the destination array, or at least provides sufficient type information at runtime to create an array of the same type.
In Kotlin, we can go one step further and actually use reified types to use information that we have at compile time to generate more specific code, such as code that create an array of a specific type (not generically, but directly with the correct element type based on the call site information). This is what toTypedArray does by reifying its type parameter.
Related
I have a document on Firestore, from which I read its fields in a fragment. Since it has many fields, I set variables in the Activity that hosts this fragment so that I can pass the data between other fragments. In order to achieve that, I realize that I have to write similar lines of codes over and over again, which got me to thinking if there is a better way.
Two possible solutions that come to my mind:
Structure all these fields in JSON format -> something that wouldn't be suitable in Firestore's document system imo
Put all these fields into a serializable data class which I keep in the activity then pass it around the bundles of fragments -> Seemed to complicated and I would still have to write it.get(foo) as bar for each of the field's of this class' constructor.
Given all these, what is the best approach? Thanks in advance.
You have a several options on how to approach this. There is none that's necessarily better than another. Ultimately, you will pick the one that best suits your needs and preferences.
You can do what you're doing now.
You can go a step further an actually check the types of the values instead of just blindly casting them (which would cause a crash at runtime if they didn't match).
You can provide a Class object to get(String, Class<T>) that can automatically map the fields to properties in a new object of type T, as long as the types also match.
You can call a variety of type-specific versions of get, such as getString()
Ultimately you will have to decide if you are going to trust what you get in the snapshot and allow errors to happen, or trust nothing and check everything. It's up to you.
Passing one or two arguments to a string in strings.xml it's possible doing this:
<string name="test">%1$s from %2$s.</string>
context.getString(R.string.test, name, name2);
result:
Pedro from Pablo.
The problem is that now I need to pass a dynamic amount of arguments, not knowing how much in each case, my idea is to get a result similar to this:
Pedro from Pablo, Paco, Peco, Puco.
It is possible to do it with a XML trick? or the only way is to do it manually with Java code?
Don't think this is possible with Android resources, because the number of arguments to substitute is always fixed, they have a fixed position and there is nothing which could represent a repeating pattern. Only Java methods support an arbitrary number of arguments with String..., where the ... means varargs.
String resources could only be used as far as defining the basic sentence and the repeating pattern, but gluing that together requires custom code - else it won't ever be a truly dynamic substitution.
Passing Information to a Method or a Constructor explains it.
You have to define it through Java only. if you know the no of dynamic content then you can define it in xml. according to your need. you have generate the second argument in loop separated by comma an then pass it to xml.
if i have levels of named parameters that needs to be passed down, i don't want to typeout the named parameter for ever method eg,
BaseText(text,options)
LargerText(text,options)
MenuText(text,options)
eventually i need to pass this to the Text Widget with the different defaults,
but to write out all of the parameters, and pass each on down seems like alot of work,
how can i get around this.
in JS i would just do
LargerText(text,options){
options = {...defaultOptions,...options}
return BaseText(text,options)
}
If Dart supported spreading map into named arguments, this would either reduce type safety, or introduce overhead checks.
Use objects to get around this. This is exactly what objects are for. With each call a compiler checks if the class is what the method expects and so all the fields present. You would not have this with maps.
ObjectBox docs suggest to use auto-assignable long ids for elements and it even has some checks based on it:
By default object IDs are assigned by ObjectBox. For each new object, ObjectBox will assign an unused ID that is above the current highest ID value used in a box. For example, if there are two objects with ID 1 and ID 100 in a box the next object that is put will be assigned ID 101.
http://objectbox.io/documentation/introduction/#Object_ID_assignment
If we have a custom key, we can add #Id(assignable = true) and it will use given field as an id.
However, I read somewhere that it adds some performance overhead and it is better to use the standard auto incremented ones whenever possible. I can't find the source now, so does anybody know if it is ok to use assignable ids for often changed objects? In addition, does ObjectBox use equals() and hashCode() somehow?
The main reasoning for using assignable ids for us is to be able to put elements using their natural long ids without manual resolving the mapping.
As I understood according to official docs and comment of Marcus Junginger (CTO of ObjectBox), there is no performance penalty when you use assignable ids.
I am porting an app from Android Java to iPhone.
In Android I used Lists/ArrayLists alot.
On iPhone I plan to use NSMutableArray.
Is there any way to define or even indicate the type of objects in an NSMutableArray.
I know one can put any type of object there, but I would like to make it more visible and transparent.
Many thanks
It's not clear exactly what you're asking.
If you just want to make it clear to the reader what sorts of object of are in the array, just name it appropriately (you can't enforce it at the language level):
NSMutableArray *arrayOfMyClasses;
If, on the other hand, you want to find out the type of an object that you're reading back from the array then you can get the underlying class using:
[obj class]
Or easily compare to other class types:
if ([obj isKindOfClass:[MyClass class]) { ... }
Tim
I assume you are looking for template pattern in Objective C. Unfortunately, it is not available in Objective C (at least directly).
You might find this question of StackOverflow.com interesting
You can only indicate a type.
for(id obj in _assets) {
NSString *className = NSStringFromClass([obj class]);
NSLog(#"%#", className);
}
Arrays are ordered collections of any sort of object. For example, the
objects contained by the array in Figure 1 can be any combination of
cat and dog objects, and if the array is mutable you can add more dog
objects. The collection does not have to be homogeneous.
Collections Programming Topics - Arrays: Ordered Collections