Welcome back.
In this lecture, I'm going to show you how you can compose tracks using other tracks, using track
and Betty.
All right, let's get started.
Let's say you want to describe any textual data using a strike like so, you also want to describe a
book and an article concept.
So you go ahead and use an old technique called the inheritance to do that.
Like so the book, an article they both inherit from the text because they both have the same set of
fields title and words inheritance uses and is a relationship.
The book is a text.
The article is a text.
And so on.
So the book, article and text all are the same text types.
Now let's take a look at it from the perspective of go.
God doesn't use inheritance.
Instead, it uses another or PEV technique called the composition.
So here, instead of inheriting from the text type, the book and article types contain it within themselves
like.
So this is called composition in your terms.
It is called and bearing.
All the types still contain the same set of fields title and words.
However, this time they are not the same types.
Each type here is a name type and they are different types.
For example, the book is not a text.
The book contains a text.
The article is not a text.
The article contains a text.
That's why it is known as a has a relationship.
Most of other or P languages support inheritance.
Fortunately, gold does not go prefers composition over inheritance.
Inheritance is a brutal technique and mostly it is not a good practice.
Using composition, you can design more robust and flexible programs.
If you want to learn more about composition versus inheritance, please check out the funny illustration
in the resources of this lecture anyway.
Now let's take a look at an example in the coning Ed.
First, I'm going to declare the text type Lexar.
Next, I'm going to declare the blacktip.
It has one more additional field ISBN, you know, each week in the world should have a different ISBN
number.
OK, now I'm going to create one of my favorite books, Moby Dick.
Let me print it as well.
Let's run it.
It just prints the book.
OK, now, instead of including the title and words feels directly in the book type.
I'm going to include them by using text like so.
However, now I cannot create and print the book is because the text is a new field, so I need to initialize
it manually to do that.
I'm going to include the title and words fields in the text field like so ISBN longus directly to the
book type, so it stays the same.
I also need to get the title and words fields from the text field like so is because they all belong
to the text field and the text field is a text track.
It is the one that contains these fields.
OK, now there are no errors and it prints the book as usual.
This is a composition because I include another track in the books.
However, you can do it better.
There is something called the Anonymous feels as its name suggests, it doesn't have a name.
Let me show you how it works.
Now I'm going to remove the name of the text field like some sort of text field here becomes an anonymous
field.
Now there is only the text type.
As you can see, the court still works, there is nothing I need to change here, so how it works,
it works because they use an anonymous feltlike.
So go ask a field name automatically using the type of the anonymous field.
What is the type of the text field?
Obviously, its type is text.
That's why this truck has a field with a text name.
You can understand that by looking at this track literal and how I retrieve the fields, I use the type
name of the anonymous field.
So what is the point of having an anonymous field?
Well, it's because it allows you to type Lescott.
I mean, you don't have to use the name of an anonymous field, for example.
I can remove them from here.
As you can see, it still works.
Doing so is equal to calling them using the text field like so.
So maybe that title equals to Mabbett that takes that title and maybe that works equals to Mabbett.
That text, that words.
Let me prove it to you here.
I'm going to change the words count to something else like so and I'm going to increment it without
using the text field lachsa.
As you can see, doing so also changes the TextField, so in summary, by using the anonymous fields,
you can type less code.
However, as you can see, you still have to use the name of the anonymous field in this track.
Literal no magic there.
OK, let me show you one last thing.
Let's say you need to include another title field here.
As you can see, there is also a title field in the text type.
So in turn, the book type has already a title field.
So what happens now?
Let's see.
As you can see, the title of the book is missing now, I think you can better understand it if I print
the value directly, like so as you can see, the title field of the book is empty by the title field.
Inside the text field is not.
So they use the same name with an anonymous field.
The parent field always takes priority.
The parents value.
Here is the book value itself.
Let me show you this in an illustration here.
When you call the title field directly on the book, you get the title on the book value.
Otherwise, if you call the title field through the text field, you get the title field of the text
value.
So as you can see, when you use an anonymous field, the parent type takes priority.
This is the parent type or in other words, the after type.
This is the embedded type or in other words, the inner type.
All right.
That's all for now.
See in the next lecture.