WEBVTT 00:01.950 --> 00:02.760 Welcome back. 00:03.240 --> 00:09.780 In this lecture, I'm going to show you how you can compose tracks using other tracks, using track 00:09.780 --> 00:10.400 and Betty. 00:10.920 --> 00:12.210 All right, let's get started. 00:13.390 --> 00:20.290 Let's say you want to describe any textual data using a strike like so, you also want to describe a 00:20.290 --> 00:22.230 book and an article concept. 00:22.810 --> 00:27.970 So you go ahead and use an old technique called the inheritance to do that. 00:28.000 --> 00:35.290 Like so the book, an article they both inherit from the text because they both have the same set of 00:35.290 --> 00:41.430 fields title and words inheritance uses and is a relationship. 00:41.920 --> 00:43.220 The book is a text. 00:43.240 --> 00:44.680 The article is a text. 00:44.680 --> 00:45.290 And so on. 00:45.640 --> 00:50.260 So the book, article and text all are the same text types. 00:50.800 --> 00:53.810 Now let's take a look at it from the perspective of go. 00:54.840 --> 00:56.750 God doesn't use inheritance. 00:56.790 --> 01:00.960 Instead, it uses another or PEV technique called the composition. 01:01.530 --> 01:09.030 So here, instead of inheriting from the text type, the book and article types contain it within themselves 01:09.160 --> 01:09.360 like. 01:09.420 --> 01:13.500 So this is called composition in your terms. 01:13.650 --> 01:15.480 It is called and bearing. 01:16.110 --> 01:20.940 All the types still contain the same set of fields title and words. 01:21.480 --> 01:24.530 However, this time they are not the same types. 01:24.810 --> 01:28.470 Each type here is a name type and they are different types. 01:28.800 --> 01:30.960 For example, the book is not a text. 01:31.260 --> 01:33.440 The book contains a text. 01:33.870 --> 01:35.730 The article is not a text. 01:35.820 --> 01:37.800 The article contains a text. 01:38.130 --> 01:41.340 That's why it is known as a has a relationship. 01:42.120 --> 01:45.910 Most of other or P languages support inheritance. 01:46.050 --> 01:51.360 Fortunately, gold does not go prefers composition over inheritance. 01:51.660 --> 01:56.160 Inheritance is a brutal technique and mostly it is not a good practice. 01:56.670 --> 02:00.990 Using composition, you can design more robust and flexible programs. 02:01.530 --> 02:07.020 If you want to learn more about composition versus inheritance, please check out the funny illustration 02:07.020 --> 02:09.760 in the resources of this lecture anyway. 02:09.810 --> 02:12.540 Now let's take a look at an example in the coning Ed. 02:14.620 --> 02:17.260 First, I'm going to declare the text type Lexar. 02:20.940 --> 02:22.920 Next, I'm going to declare the blacktip. 02:24.750 --> 02:30.990 It has one more additional field ISBN, you know, each week in the world should have a different ISBN 02:30.990 --> 02:31.380 number. 02:31.860 --> 02:35.160 OK, now I'm going to create one of my favorite books, Moby Dick. 02:36.910 --> 02:38.080 Let me print it as well. 02:43.030 --> 02:43.730 Let's run it. 02:44.140 --> 02:45.400 It just prints the book. 02:45.580 --> 02:50.660 OK, now, instead of including the title and words feels directly in the book type. 02:50.680 --> 02:53.650 I'm going to include them by using text like so. 02:54.830 --> 03:02.690 However, now I cannot create and print the book is because the text is a new field, so I need to initialize 03:02.690 --> 03:04.660 it manually to do that. 03:04.710 --> 03:12.290 I'm going to include the title and words fields in the text field like so ISBN longus directly to the 03:12.290 --> 03:14.260 book type, so it stays the same. 03:14.900 --> 03:22.190 I also need to get the title and words fields from the text field like so is because they all belong 03:22.190 --> 03:26.320 to the text field and the text field is a text track. 03:26.690 --> 03:29.270 It is the one that contains these fields. 03:29.780 --> 03:33.780 OK, now there are no errors and it prints the book as usual. 03:34.430 --> 03:38.510 This is a composition because I include another track in the books. 03:39.320 --> 03:41.040 However, you can do it better. 03:41.150 --> 03:47.060 There is something called the Anonymous feels as its name suggests, it doesn't have a name. 03:47.270 --> 03:48.720 Let me show you how it works. 03:48.890 --> 03:55.160 Now I'm going to remove the name of the text field like some sort of text field here becomes an anonymous 03:55.160 --> 03:55.550 field. 03:55.940 --> 03:58.310 Now there is only the text type. 03:59.260 --> 04:05.350 As you can see, the court still works, there is nothing I need to change here, so how it works, 04:05.620 --> 04:09.040 it works because they use an anonymous feltlike. 04:09.040 --> 04:13.810 So go ask a field name automatically using the type of the anonymous field. 04:14.230 --> 04:16.390 What is the type of the text field? 04:16.960 --> 04:19.320 Obviously, its type is text. 04:19.900 --> 04:22.930 That's why this truck has a field with a text name. 04:23.350 --> 04:30.700 You can understand that by looking at this track literal and how I retrieve the fields, I use the type 04:30.700 --> 04:32.430 name of the anonymous field. 04:33.250 --> 04:36.430 So what is the point of having an anonymous field? 04:37.090 --> 04:40.690 Well, it's because it allows you to type Lescott. 04:41.140 --> 04:45.430 I mean, you don't have to use the name of an anonymous field, for example. 04:45.430 --> 04:47.110 I can remove them from here. 04:47.830 --> 04:49.450 As you can see, it still works. 04:49.720 --> 04:54.000 Doing so is equal to calling them using the text field like so. 04:54.790 --> 05:03.220 So maybe that title equals to Mabbett that takes that title and maybe that works equals to Mabbett. 05:03.220 --> 05:05.020 That text, that words. 05:05.170 --> 05:06.880 Let me prove it to you here. 05:06.910 --> 05:12.520 I'm going to change the words count to something else like so and I'm going to increment it without 05:12.520 --> 05:14.290 using the text field lachsa. 05:15.180 --> 05:22.400 As you can see, doing so also changes the TextField, so in summary, by using the anonymous fields, 05:22.610 --> 05:24.190 you can type less code. 05:24.560 --> 05:29.520 However, as you can see, you still have to use the name of the anonymous field in this track. 05:29.570 --> 05:31.280 Literal no magic there. 05:31.980 --> 05:34.250 OK, let me show you one last thing. 05:34.460 --> 05:37.880 Let's say you need to include another title field here. 05:38.570 --> 05:42.250 As you can see, there is also a title field in the text type. 05:42.470 --> 05:46.130 So in turn, the book type has already a title field. 05:46.220 --> 05:47.450 So what happens now? 05:47.630 --> 05:48.200 Let's see. 05:49.390 --> 05:55.810 As you can see, the title of the book is missing now, I think you can better understand it if I print 05:55.810 --> 06:02.350 the value directly, like so as you can see, the title field of the book is empty by the title field. 06:02.350 --> 06:04.390 Inside the text field is not. 06:04.390 --> 06:07.960 So they use the same name with an anonymous field. 06:07.990 --> 06:11.170 The parent field always takes priority. 06:11.170 --> 06:12.160 The parents value. 06:12.160 --> 06:14.000 Here is the book value itself. 06:14.020 --> 06:17.560 Let me show you this in an illustration here. 06:17.590 --> 06:22.770 When you call the title field directly on the book, you get the title on the book value. 06:23.320 --> 06:29.470 Otherwise, if you call the title field through the text field, you get the title field of the text 06:29.470 --> 06:29.890 value. 06:30.400 --> 06:35.620 So as you can see, when you use an anonymous field, the parent type takes priority. 06:35.920 --> 06:39.300 This is the parent type or in other words, the after type. 06:39.850 --> 06:43.340 This is the embedded type or in other words, the inner type. 06:43.660 --> 06:44.210 All right. 06:44.380 --> 06:45.230 That's all for now. 06:45.250 --> 06:46.270 See in the next lecture.