The documentation says
Clone-
"Creates a modifiable clone of the System.Windows.Freezable, making deep copies
of the object's values. When copying the object's dependency properties,
this method copies expressions (which might no longer resolve) but not animations
or their current values."
CloneCurrentValue-
"Creates a modifiable clone (deep copy) of the System.Windows.Freezable using
its current values."
It means both do deep copy. Then what is the difference?
If I understand the documentation correctly, Clone also copies the binding expressions. So if a property of the object is bound, it remains bound in the copy.
CloneCurrentValues, on the other hand, only copies the current values, as the name implies. Bindings are not kept, so the values in the copy won't be updated if the source of the binding is modified.
Related
Can React.useMemo second argument array contains an object?
I ask this question because I have an expensive computation based on an object's value.
I don't know should I expand the object, or just simply pass the object into that array.
It is possible to use an object as 2nd argument. But it depends on how the object behaves. If there will be always a new instance of this object each time the affected value(s) has changed, React.useMemo will be able to detect the change. Since React.useMemo will only do an instance compare in case of an object, it will not detect changes within that object if the instance remain the same. If the instance changes more often then the affected properties, it would be better to extract only the required properties and hand them over individually. This will ensure the calculition will only be done if need.
I have a UITableViewController that can display content from any of three different arrays. In Objective-C, what I would typically do is set a property on the TableVC that is a reference to the array to be displayed. Then the TableVC would not only be able to show the contents of that array, but also handle user-directed editing of the array, say, if they deleted or re-ordered elements.
The way it seems to work in Swift when I set a property on my TableVC to an array is that it is a copy of my model's array, not a reference. And while this is fine in certain contexts, in my application it seems bad for two reasons: 1) my arrays are enormous, thousands of elements, and copying huge arrays over and over again seems wasteful; 2) small edits are tougher to handle: I have to communicate back to the model about element deletion or re-ordering and make sure my view and model arrays stay in sync.
Setting a property that's a reference to an array is what I want to do, but there's no such thing as an inout object property in Swift.
What is the correct way for me to handle this?
Thanks.
Swift Arrays are not actually copied every time you assign them; instead, they are copy-on-write. What this means is that the array is only copied when something mutates it; until that point, you only copy a reference, like with Objective-C.
With that said, if you need reference semantics, the NSArray class from Objective-C is still available in Swift. You can also create your own class type that wraps an Array if you prefer not to use the Objective-C bridge.
So I've been learning crystal without a ruby background and noticed the api docs have #dup and #clone for basically copying an array.
What exactly is the difference between the two? The api says #dup shallow copies the array whilst #clone deep copies said array. I'm not sure what exactly that entails and which one I should be using.
#dup will duplicate the array in memory, that is the list of items it contains, but it won't duplicate the items themselves. Mutating the new array won't affect the previous array(e.g. push, pop) but mutating any of its items will affect the item of the previous array since the items are the same objects.
#clone will duplicate the array list in memory, but also its items, by calling #clone recursively. This is a full clone of the original array. Mutating anything, even a deep nested object, won't affect the original content.
Note that this applies to any object, not just arrays, and that the behavior can be customised by overriding the methods in your own objects.
That being said, it only applies for arrays of objects (e.g. class instances). For arrays of primitives (integer, float, struct...) the items will be copied along with the array list by #dup.
I've been having some difficulty understanding exactly what happens when a model is manipulated in Backbone.
1) When calling #get on a Collection to "grab" a model, is the model the same model as the Collection's model? (e.g., updating the model will update the Collection's model)
2) If a model is added to various collections, do all of those collections contain the actual model (or a "copy" of the model)? It seems to me that it's the "copy" because when I try to destroy a model that has been added to various collections, not all the models in the various collections are destroyed.
Thanks! Appreciate any insights.
Like every other object in Javascript, Backbone objects are 'passed by a copy of the reference'. The best way to think about this is that javascript has a piece of data in memory, and variables are nothing more than pointers to those bits of data. When you set one variable equal to another, what you really get is two copies of the pointer, both pointing at the same piece of data in memory. So, applying this to your question:
Yes. When you 'get' the model, what you 'get' from backbone is a
pointer to the place in memory where the object is stored. Now you
have two pointers (one in the collection and one in your variable), and you can perform operations on either of them
and either will perform that operation on the same piece of data in
memory.
Kind of. Each collection has, again, a pointer to the same
object/model. When you remove that pointer from a collection, the
other pointers remain pointing to the same piece of memory, and that
memory is not erased, because it's still being pointed at from other
collections. model.destroy() will trigger a destroy event on both
the model and the collection it is holding a pointer to in it's
collection attribute. However, the model can not hold pointers
to multiple collections if it is a part of more than one collection.
So on your destroy event, it is removed only from the collection it was last assigned to - the one it holds in the model.collection attribute. Ordinarily, when no variables are holding pointers to the piece of data in memory, that memory will be erased, however, in this case, because your other collections have pointers to the model, the model remains in memory as a part of those collections.
I am not sure the practical value of such a thing, but I am wondering if in for example Java, an object can be instantiated so that if a variable holds a reference to it, no other variable can do so unless the first variable no longer does. The object could only be in a single list. Intuitively, this would correspond more to real life objects that can only be in one place at a time.