Is there any language or language feature where you can re-call the constructor on an instance of a class and is there any reason why you don't want to do this?
For example,
//let's say you have this:
var person = new PersonModel("John", "Doe");
// is it possible to do this:
person.constructor("Jane", "Doe");
Reason:
I'm writing code and I have a case where I have reusable objects (data objects in collection) and being able to update the object through the constructor parameters would be an easy way to update the data object for me for right now in my specific case. I know I can break the code up if to do the same thing.
Longer Reason:
I'm creating a Collection View in C# in Xamarin that wraps NSCollectionView (think grid layout in HTML) and I wanted to prepopulate the grid with ten items for the user to see and click on. So these are placeholder items because the Collection View won't be created unless it has data. But the items are like images. They have to be loaded. So I have to have data, even dummy data, to create the view.
So, I created the Data Value Objects or Data Models to create the grid of items. Then when the user clicks on the grid item I can load in an image. So since I already have the data object and simply need to load in content I want to reuse it. It wouldn't mess up the layout or any animations or anything else that creating a new object might.
It would take one line of code if I could reuse the object compared to about 100 lines of code to break out the constructor and other related things like create a new instance with the new information.
I've done the break code in the past. It occurred to me to ask if this is possible bc I had similar use cases like this a few times ...and because JavaScript breaks so many paradigms that maybe it was possible in some languages. Because JS has the myMethod.apply() and if a constructor is just a method would object.constructor.apply() work.
__init__
) is really just an ordinary method which is automatically called after creating an instance. You can call it manually later if you want. Wouldn't recommend it though.