1 00:00:06,420 --> 00:00:06,750 Hello. 2 00:00:07,890 --> 00:00:11,310 In this video, we're going to talk about Topic 19. 3 00:00:11,730 --> 00:00:13,620 What is pattern matching? 4 00:00:14,220 --> 00:00:20,850 Pattern matching is a technique where you test an expression to determine if it has certain characteristics. 5 00:00:21,480 --> 00:00:25,530 The easiest way to understand popular matching is with an example. 6 00:00:25,920 --> 00:00:31,470 Let's say I want to run some code if some value is no and other if it isn't. 7 00:00:42,440 --> 00:00:44,390 This code is pretty straightforward. 8 00:00:44,810 --> 00:00:46,340 There is only one problem. 9 00:00:46,700 --> 00:00:54,380 We don't know what this object is exactly, and it may turn out that it is some type of quality operator 10 00:00:54,380 --> 00:01:00,170 of an old and that it actually does something else than simply taking if the value is new. 11 00:01:00,530 --> 00:01:06,380 To avoid this, we can use our first pattern from pattern matching the nortec pattern. 12 00:01:08,690 --> 00:01:10,490 Well, it's not rocket science. 13 00:01:10,970 --> 00:01:18,170 This pattern just works if an object is known, but pattern matching can give us much, much more ability. 14 00:01:18,890 --> 00:01:21,440 Let's walk through some more complex examples. 15 00:01:21,890 --> 00:01:25,130 One of the most commonly used buttons is type test. 16 00:01:25,640 --> 00:01:29,180 I want to run some code if a variable is of some type. 17 00:01:29,570 --> 00:01:33,410 Moreover, if it is, I want to cast it to this type. 18 00:01:33,860 --> 00:01:35,420 We felt pattern matching. 19 00:01:35,570 --> 00:01:38,060 I would need to write something like this. 20 00:01:54,440 --> 00:01:55,620 Here I am talking. 21 00:01:55,640 --> 00:02:02,330 If the object is strong, I could do it even simpler by using the East Keyword, which we learned about 22 00:02:02,330 --> 00:02:02,900 before. 23 00:02:06,640 --> 00:02:12,880 So first, I'd check if the object is trying, and then I passed it to string with pattern matching. 24 00:02:13,060 --> 00:02:15,010 I can do it all in one line. 25 00:02:20,130 --> 00:02:21,420 This is quite convenient. 26 00:02:21,810 --> 00:02:26,940 These are string variable will be available only if this object is a string. 27 00:02:27,270 --> 00:02:32,280 So I would not be able to use it anywhere else than inside this if statement. 28 00:02:32,850 --> 00:02:37,260 We can also object some particular properties of the target object. 29 00:03:03,170 --> 00:03:10,940 Here we checked if an object is a pet with weight larger than 10000 and put equal to fish. 30 00:03:11,870 --> 00:03:17,060 All right, the next type of pattern matching is comparing discrete values. 31 00:03:17,420 --> 00:03:21,500 This is very similar to using plain old sweet statement. 32 00:03:27,570 --> 00:03:28,980 Here I have a method. 33 00:03:29,160 --> 00:03:36,090 Taking a string that should represent a number and another string saying what type of number it is. 34 00:03:36,300 --> 00:03:42,240 So, for example, int decimal or float, depending on the second parameter. 35 00:03:42,420 --> 00:03:46,080 I want to convert the first parameter to the given type. 36 00:03:46,290 --> 00:03:48,090 Let's do it with pattern matching. 37 00:04:08,030 --> 00:04:10,370 Please notice this special case. 38 00:04:10,850 --> 00:04:16,280 This is a discard pattern, and it works similarly as default in this which statement. 39 00:04:16,520 --> 00:04:22,610 It will be executed if the type parameter is not equal to any of the specified values. 40 00:04:23,690 --> 00:04:26,660 Let's get two more complex types of pattern matching. 41 00:04:27,110 --> 00:04:29,570 The next one is a relational pattern. 42 00:04:30,020 --> 00:04:34,340 It allows us to check how a given value compares two constants. 43 00:04:52,320 --> 00:04:57,180 As you can see, called, using pattern matching is very simple and readable. 44 00:04:57,780 --> 00:05:04,980 The cool thing about pattern matching is that it has very good visual studio and compiler support. 45 00:05:05,190 --> 00:05:07,980 And we get an error when we do something silly. 46 00:05:08,430 --> 00:05:11,520 For example, let me add some more cases here. 47 00:05:20,070 --> 00:05:25,080 This code doesn't compile because the last two cases are unreachable. 48 00:05:25,560 --> 00:05:28,710 The cases are executed from top to bottom. 49 00:05:29,250 --> 00:05:38,040 So when the eight parameter S10 will hit, the less than 20 case will never reach the less than 11 case. 50 00:05:38,490 --> 00:05:42,030 So what we should do is fix the order of the cases. 51 00:05:48,710 --> 00:05:51,630 Right now, this will work as expected. 52 00:05:52,220 --> 00:05:56,750 This code actually demonstrates one more pattern, no logical pattern. 53 00:05:57,140 --> 00:05:58,280 We used it here. 54 00:05:58,370 --> 00:06:02,810 Where we checked it out is less than 20 and more than 60. 55 00:06:03,560 --> 00:06:06,680 We can also use pattern matching quilted construction. 56 00:06:06,890 --> 00:06:08,480 What we learned about before. 57 00:06:41,970 --> 00:06:48,600 As you can see, we deconstructed the object into its components, and we used them to define a pretty 58 00:06:48,600 --> 00:06:49,630 complex logic. 59 00:06:50,190 --> 00:06:52,710 We could all meet the parameter names. 60 00:06:52,830 --> 00:06:56,360 But personally, I would rather leave them for readability. 61 00:06:59,680 --> 00:07:01,780 As you can see, this also works. 62 00:07:02,230 --> 00:07:06,730 We can also mix the construction with taking particular properties. 63 00:07:15,980 --> 00:07:16,670 All right. 64 00:07:17,090 --> 00:07:21,110 So we learned some of the most basic usages of pattern matching. 65 00:07:21,470 --> 00:07:27,680 There is also a question when to use them and when to use plain old if and switch statements. 66 00:07:28,190 --> 00:07:32,660 In my opinion, you should simply use those that you find more reliable. 67 00:07:33,150 --> 00:07:36,860 You can also mix both to get what's best in any of them. 68 00:07:37,580 --> 00:07:44,120 To summarize, pattern matching is a technique where you can test an expression to determine if it has 69 00:07:44,120 --> 00:07:45,770 certain characteristics. 70 00:07:46,520 --> 00:07:51,380 During the interview, you can be asked about one of the most popular patterns. 71 00:07:51,920 --> 00:07:58,280 How can we check if an object is of a given type and customized to this type in the same line? 72 00:07:58,970 --> 00:08:01,460 For that, we can use pattern matching. 73 00:08:01,730 --> 00:08:06,080 For example, we could write if Param is string text. 74 00:08:06,380 --> 00:08:12,830 This way will cost the around to the string variable called text, but only if this object is of type 75 00:08:12,830 --> 00:08:13,430 strength. 76 00:08:14,000 --> 00:08:14,630 All right. 77 00:08:14,990 --> 00:08:16,340 That's it for this lecture. 78 00:08:16,700 --> 00:08:19,550 Thanks for watching, and I'll see you in the next one.