High this the strings of the heart and welcome to the next value of this Mordieu will all functions
in this video will really talk about Vawter extension function in case of Cartland.
Now the purpose of extension function is to add a new function to the existing class.
So it simply adds a particular function to a predefined class without declaring it inside the class.
And the new functions actually behaves just like the static functions in case of Java.
Now practically also during the completion the Cartland compiler also converts these functions into
the static functions internally that happens behind the curtain.
So we do not see what happens behind the curtain but exactly this happens.
So that does not check out the extension function inside the intelligent ID.
So first I would simply create a class of let's see student insert this I would simply define a function.
So here I have a method of has first.
So if the student has passed the exam or not will be determined inside this method.
So the Mock's is greater than 40 then going through is three and four is because the student has failed
not to simplify this code by simply removing the redundant called.
So here I'm simply returning if the marks is greater than 40 then returned true.
Is that again false right now insert the main method I simply define a chord.
So here I have simply written the code by simply defining the object of the other in class and then
here.
I'm simply passing the marks of the student as 57.
So if the student has first exam or not will be determined with the help of discord right now.
Suppose this is our.
Now this class is actually our own class right.
So we are the sole owner of this class of student.
Now suppose this class is already defined and later on in your application you need one more to function
inside the class of student.
So so without defining the function right.
Inside the class what you can do is you can simply create a One more function like C function has got
scholarship or not.
Or let's say is Scollard.
So if the student has secured the marks greater than 95 then he is actually a scholar as he is not.
So this function should be the part of law student.
But for that we have to find this method inside this class of student but we don't want to do it.
So what they really do is I would simply use the concept of extension function where the syntax is let's
see.
You need to use the class name of then come on and see and paste it here followed by the dot operator.
So here Varda of then is I've simply used student dord is scholar.
So whenever I use this syntax and this function is Scollard will behave like a function present inside
the class of student.
So here I can simply use.
Let's see Parentline scholarships status.
Student dord is scholar right.
And simply Pazner the marks of the student as let's say 57.
So here if I add in the call right now.
Then in the output we will get something like that.
Scholarships stators either true or false.
So here we get the scholarships tutors as follows.
And the pastor has as you know this scholarship students as far as it's coming from the extension function
right here like this.
Now if I tell you that this is actually not the practical usage of the extension function.
So what he would have he saw right now was just an explanation of the extension function.
How do we define it.
The syntax is like this.
Now let us come to the point where it has defined a function and check out the practical usage of the
extension function so that it's not genes are called.
Now suppose I define a string see SD or one other type of string equal to hello.
And now let us define one more string.
So here I've simply defined two string values string one and string two other type of hello and void
value.
And now I suppose if I define one more SDR tree of the type of string equal to.
Let's see.
Right now suppose later on in our program I want to combine these three strings.
That is I'm going to append these three strings together.
For example we can simply use geometry equal to SDR three less SDR even less SDR.
Do like this.
So this will work fine.
So in this way we can simply append three strings.
Now suppose I want to define a function that is I want to use the string plus to add these three strings.
Right.
So what they will do is simply try to find the solution by using SDR 3 dord.
Let's say I have any Metford That simply adds all the strings together or not.
So here I don't have any matter that takes two more metres and simply add all the strings together.
So what we do is down the side.
I would simply create the extension function that is let us create a function inside which we will add
all the strings together right.
So I will simply create an extension function for the class of string string dord.
Let's see and right and now as a it I simply simply pass.
Let's say as diehard latest string one of that type of string of their type of string again.
Right.
And this simply return appended string.
And now here I will simply use a written statement and I would simply add this.
Less is even less true.
Now I will explain right now why it is happening.
So I will simply use your SD card or add and simply pass SDR.
When an SD card too right and now let us print the cord by simply using the print LNA statement SD or
treat the statement from here and pasted here and now let us run the code and lets see what happens.
So in the output we get.
Hey hello world.
So what exactly is happening here.
So here I have simply defined the extension function of.
Now this extension function and is now a part of this class of string.
Now this string is actually the predefined class who scored.
We have not written.
It is already existing in our SDK so we don't know how to modify this class.
But in Codlin there is a way to do it.
We can simply add a function inside a predefined class so this function is now is a part of this class
right of the string right.
And now here it takes to be the meter's string and string to.
And here I'm calling this method with the help of this SDR tree.
Now your tree is defined here which is actually of their type of string.
Now as already said everything in Gartley is actually an object.
So this string tree is actually an object so very simply calling objects don't act Metford.
So a string object don't add method right just in a way that we call any other method with the help
of the object.
So this is Tier 3 simply cause this method and simply bus stupid leaders and inside this method I'm
using this list is endless as to now this keyword is actually referring to this SDR tree that actually
holds the reference for the string tree.
Now string tree is actually has a value of here as one has a value of hello and two has the value of
void.
So we are agreeing.
Hey hello way to get that appended and returned inside the print L-N mattered and we're getting the
output as hey hello world write.
And now let us take one more example.
Let's straight to find a greater number with the help of the integer class Val X like C equal to 6 and
then I would simply use Val VEIDT equal to 10.
Now these two are actually of their type of right.
So don't forget this.
It is also their type of end.
Now here there is no predefined function where we can simply call legacy X..
Dog find the greater number like that or greater number.
We don't have any Metford inside the integer class that does this in a class so for.
Again on that side I will simply define the extension function for the class of NBG.
Dark large see greater value.
And here I will simply pass one.
I mean that because we are having to NBG the values S.N..
So the first one will fall here and the second one will pass on with the help of all the other number
of their type of.
And it simply returns the integer value right.
This is greater than the other.
So here I've simply completed my code.
So inside this method I have written the if condition.
If this is better than other number then simply return the correct number it is returned the other number
right.
So if we are calling X Dorte greater value and simply pass the other value as by.
Right.
So here we will get the value as well greater value equal to extraordinary Ravelli Why not simply print
out let's see greater value.
So when the street is executed exhorted revalue is simply going to call this function in dark revalue
which is not a part of this interior glass.
Right.
And now here this refers to the value of x the object that is calling this method the value of that
object is actually stored inside this key word.
Right.
So this refers to the correct reference of the object.
And now the other one is actually the parameter that we are passing here.
With the help of y.
Very well.
So if the current value is greater than the other value then simply return this otherwise return the
other value which is not a greater value.
So let us note in the chord.
So here in the output we have as a value which is a greater number among 6 and 10 right.
So this is the way you can use the extension function in case of Cartland.
Now what are the advantages of using the extension function.
Let's check it out.
So the extension function has few properties such as they can become part of your own plussed example
student employee user and so on.
And the other thing is that they can become part of the predefined classes such as add a string integer
Gad or any predefined classes that comes with that Gartin is the key right.
So all of this remember this.
You can make the part of your custom class such as student Derocher you saw in this video and also for
the predefined classes such as string and and so on.
And now what are the advantages of using the extension function is that it simply reduces the code and
it makes the code much more cleaner and easier to read Serj simply reduces scored by allowing you to
add your own custom methods inside some class so you don't have to modify your old classes or the predefined
classes for just one function.
So this is all for this video about the extension function in case of Cartland.
Thanks for watching and have a good day.