'This' can be hard to speak about because it have a meaning in a sentence. The keyword 'This' will be placed inside simple quote to differentiate the word used in a sentence and the keyword we will try to put light on in this blog post.
'This' is a keyword that represent the current object in Object-oriented programming (OOP).
As 4D make a big step in OOP, I think it is good to take some time to analyse how 'This' work in 4D. I will not do comparaison with other programming languages here but please feel free to comment the post to add your remarks.
How can we have access to 'This' in 4D? For that, we must have an object with a custom member function. But how do we define member function in 4D? Here is how we assign a member function to an object :
Here we see that I create an object and assign it to the $person1 local variable.
First question, when my object is created? At line 1 or 2?
Actually the object is created when you execute the "New object" command. The "C_OBJECT" command only declare that the $person1 local variable will be an object variable. In fact, here, the variable will only contains a reference to the object created via the "New object" command. Before the "New object" command, the $person1 is undefined. See my Tip #8 for more informations about object references.
I then assign a formula to the "sayHello" property of my object. I'm doing that thanks to the "Formula" command (before v17 R6 it was called "New formula" ). If a formula is assigned to a property, the property become a member function. You can now call the member function by writing "$person1.sayHello()". It will execute the "MF_sayHello" method.
Now come the interesting part where 'This' enter in the game. We see that our object doesn't have any properties yet.
Let's add a name to our person :
We now have one property named "name".
If we look at the "MF_sayHello" method we will see how simple is to use the 'This' :
Here 'This' represent the object that have called the member function "sayHello". In this situation 4D automatically bind 'This' to the current object that have called the member function.
What if we want to have the control over what 'This' in binded to?
This is possible thanks to 2 member functions of our custom member function. Yes, a member function is a formula and a formula have 2 member functions available : "call" and "apply". Those 2 member functions accept, as the first parameter, an object that will be binded to 'This'. The only difference is how some parameters will be passed to the member function (if any)
"call" will accept a serie of parameters separated by a ";" while "apply" will accept a collection parameter that contain the serie of parameters to pass to the member function. The last one allow to write more generic code.
Let's look at one example :
We see here how I use the member function of the "$person1" object to assign addresses to "$person2" and "$person3" objects. Notice that "$person2" and "$person3" don't have the "setAddress" member function.
Ok that's all for now. I'm sure this will talk to many developers coming from OOP languages. For 4D developers it can be new but it open a whole new world of possibilities. We can start to create sort of class inside 4D in a really simple way. I have no ideas if in the future we will have more "class like" stuff like "private" and "public" attributes and "constructor", etc. But let the future bring us some more surprise.
P.S. And I almost forgot, here is a small HDI database that demonstrate all what I speak in this post. I let you play with it : https://www.dropbox.com/sh/8jel0sv6kbo87ae/AAAlZbWAudx2VACEhK2qXnwFa?dl=0