1 00:00:00,400 --> 00:00:01,640 Hello. 2 00:00:02,240 --> 00:00:06,560 Today we will review the implementation of ifTrue:ifFalse: 3 00:00:06,720 --> 00:00:09,920 If you've been attentive and I'm sure you have, 4 00:00:10,080 --> 00:00:14,320 you surely noticed that when I suggested examples for Not, 5 00:00:14,480 --> 00:00:17,920 I said you could implement Not, Or and ifTrue:ifFalse: 6 00:00:18,080 --> 00:00:21,320 But in the solution, I never mentioned ifTrue:ifFalse: 7 00:00:21,480 --> 00:00:23,720 We'll solve this problem now. 8 00:00:24,440 --> 00:00:28,760 In effect, in Pharo, conditions are messages. 9 00:00:28,920 --> 00:00:30,480 Here's an example. 10 00:00:30,640 --> 00:00:34,480 We have a weather class: "Weather isRaining". 11 00:00:34,640 --> 00:00:36,600 This means that if it rains... 12 00:00:37,240 --> 00:00:40,880 If it's true, I'll take my umbrella. 13 00:00:41,040 --> 00:00:43,400 If it's false, I'll take my sunglasses. 14 00:00:43,760 --> 00:00:45,840 With ifTrue:ifFalse: 15 00:00:46,000 --> 00:00:50,960 You see the two colons? This means they're keyword messages. 16 00:00:51,120 --> 00:00:54,440 The message ifTrue:ifFalse: is a keyword message 17 00:00:54,600 --> 00:00:57,320 and it's sent to a boolean instance. 18 00:00:58,160 --> 00:01:02,720 In reality, it is heavily optimized by the compiler and not really sent, 19 00:01:02,880 --> 00:01:05,640 but conceptually speaking it is. 20 00:01:05,800 --> 00:01:10,960 So if you want to redefine the method ifTrue: 21 00:01:11,120 --> 00:01:12,640 ifFalse: 22 00:01:12,800 --> 00:01:14,760 You can do this in Pharo. 23 00:01:14,920 --> 00:01:17,240 I'll explain the technique now. 24 00:01:17,400 --> 00:01:20,880 The ifTrue:ifFalse: method is so important 25 00:01:21,040 --> 00:01:23,520 and we need it to go so fast, 26 00:01:23,680 --> 00:01:26,800 that the function is not actually invoked. 27 00:01:27,160 --> 00:01:29,400 The compiler makes jumps inline instead. 28 00:01:29,560 --> 00:01:33,240 But for the purpose of this class, we'll see how it functions. 29 00:01:33,400 --> 00:01:35,960 I want you to propose an implementation. 30 00:01:36,120 --> 00:01:37,800 You know about blocks. 31 00:01:37,960 --> 00:01:40,920 You know what we did with True and False, 32 00:01:41,120 --> 00:01:42,720 and with Or and Not. 33 00:01:42,880 --> 00:01:45,440 You can do it with ifTrue:ifFalse: 34 00:01:45,600 --> 00:01:49,960 In sum, if my receiver is false, I return 5. 35 00:01:50,120 --> 00:01:52,880 And if my receiver is true, I return 3. 36 00:01:53,600 --> 00:01:56,920 Once again, we have objects, messages and blocks. 37 00:01:57,080 --> 00:02:00,000 It's a bit repetitive, but that's the way it is. 38 00:02:00,600 --> 00:02:03,480 You remember that brackets... 39 00:02:05,960 --> 00:02:08,080 freeze the execution 40 00:02:08,240 --> 00:02:10,800 of the expression they contain, 41 00:02:10,960 --> 00:02:15,360 and that "value" kicks the execution of a frozen code. 42 00:02:15,520 --> 00:02:18,680 With this, and Not and Or, 43 00:02:18,840 --> 00:02:21,480 you should be able to implement your conditions. 44 00:02:21,640 --> 00:02:25,280 Likewise, you should be able to implement ifTrue:ifFalse: 45 00:02:25,440 --> 00:02:27,960 I'll give you a few seconds to think. 46 00:02:29,000 --> 00:02:30,720 How is it implemented? 47 00:02:30,880 --> 00:02:34,360 In exactly the same manner as Or and Not. 48 00:02:34,520 --> 00:02:37,280 In the True class, I have a method called 49 00:02:37,440 --> 00:02:39,920 ifTrue: ifFalse: and two arguments. 50 00:02:40,080 --> 00:02:44,280 One block for True and one block for False. 51 00:02:44,440 --> 00:02:45,520 What do I do? 52 00:02:45,680 --> 00:02:48,320 When I'm in the True block, I execute True. 53 00:02:49,600 --> 00:02:52,160 And when I'm in the False class, 54 00:02:52,320 --> 00:02:56,240 I have the same method with the same two arguments, 55 00:02:56,400 --> 00:03:00,160 but since I'm in the False class, I execute the False block. 56 00:03:00,520 --> 00:03:03,080 I have the booleans' implementation. 57 00:03:03,240 --> 00:03:04,440 How does it work? 58 00:03:05,400 --> 00:03:06,520 Like this. 59 00:03:06,680 --> 00:03:10,160 When I send a message to my boolean, 60 00:03:10,320 --> 00:03:12,560 which is in fact True, 61 00:03:12,720 --> 00:03:16,480 I send ifTrue: do something True or do something False. 62 00:03:16,640 --> 00:03:20,520 Then I look up ifTrue:ifFalse: in the True class, 63 00:03:20,680 --> 00:03:21,920 It's right here. 64 00:03:22,080 --> 00:03:25,440 It says to execute the True block, so I do. 65 00:03:25,600 --> 00:03:29,600 Now I send the message "B is a boolean: False." 66 00:03:29,760 --> 00:03:33,800 I send the message and look here. I find ifTrue:ifFalse: 67 00:03:33,960 --> 00:03:38,760 It says to execute the False block, and that's what I do. 68 00:03:39,200 --> 00:03:43,480 You cannot test this by putting a breakpoint in ifTrue:ifFalse: 69 00:03:43,640 --> 00:03:46,400 It's optimized and the system won't accept it. 70 00:03:46,560 --> 00:03:50,840 But what I do suggest you try, is implement in the same manner: 71 00:03:51,000 --> 00:03:52,800 ifFalse: or ifTrue: 72 00:03:52,960 --> 00:03:55,080 ifTrue: a Block 1, 73 00:03:56,160 --> 00:04:00,680 and ifFalse: A Block 2. Then implement them. 74 00:04:00,840 --> 00:04:03,280 Use a breakpoint and experiment. 75 00:04:03,880 --> 00:04:07,920 So what have we shown with this example? 76 00:04:08,080 --> 00:04:11,640 We saw that sending a message selects the right method. 77 00:04:11,800 --> 00:04:16,320 Here we decided to let the receiver decide its behavior. 78 00:04:16,480 --> 00:04:21,400 And we saw that brackets freeze computation, 79 00:04:21,560 --> 00:04:24,840 and that "value" forces the execution of a frozen code.