1 00:00:07,040 --> 00:00:10,440 Hello. This week, we'll get to the heart of the matter, 2 00:00:10,600 --> 00:00:12,160 about syntax especially. 3 00:00:12,320 --> 00:00:17,560 This class will be mostly about dealing with the messages of Pharo's syntax, 4 00:00:17,720 --> 00:00:19,560 which is most of Pharo. 5 00:00:20,400 --> 00:00:23,840 In Pharo, there are three main things: 6 00:00:24,000 --> 00:00:28,040 objects, messages, and closures called blocks. 7 00:00:28,200 --> 00:00:30,200 There'll be a course about blocks. 8 00:00:30,360 --> 00:00:33,640 You can cover almost the entire syntax of Pharo with this. 9 00:00:33,800 --> 00:00:37,360 Pharo's syntax stems from that of Smalltalk 10 00:00:37,520 --> 00:00:40,760 which was designed to teach children how to program. 11 00:00:40,920 --> 00:00:44,400 It's about writing programs which are like short sentences 12 00:00:44,560 --> 00:00:46,040 a child might write. 13 00:00:46,200 --> 00:00:49,320 It's also about minimizing the number of parentheses. 14 00:00:49,480 --> 00:00:50,960 We'll see how it works. 15 00:00:52,040 --> 00:00:56,320 For instance, let's take a tile on openstreetmap. 16 00:00:56,480 --> 00:00:59,520 I want to open it in a graphic window. 17 00:00:59,680 --> 00:01:02,400 This is an example of the syntax. 18 00:01:02,560 --> 00:01:06,000 I use parentheses because I want to get a png file first. 19 00:01:06,160 --> 00:01:09,520 I convert the png file into a Pharo graphic object 20 00:01:09,680 --> 00:01:14,920 before opening it in a window: do it with Pharo and you'll get the result. 21 00:01:15,080 --> 00:01:18,200 It'll probably be water as there are many water tiles. 22 00:01:19,120 --> 00:01:23,040 In Pharo's syntax, there are three types of messages 23 00:01:23,200 --> 00:01:26,240 to minimize the number of parentheses. 24 00:01:26,400 --> 00:01:30,480 There are unary, binary, and keyword messages. 25 00:01:30,640 --> 00:01:31,880 Let's see about them. 26 00:01:32,520 --> 00:01:37,040 With unary messages, you have one receiver then the method's name. 27 00:01:37,200 --> 00:01:41,600 With binary messages, one receiver, the selector, then an argument. 28 00:01:41,760 --> 00:01:46,880 With keyword messages, one receiver, then one argument or arguments. 29 00:01:47,040 --> 00:01:51,000 You can distinguish them thanks to the colon here. 30 00:01:51,160 --> 00:01:52,560 We'll study this. 31 00:01:53,560 --> 00:01:57,960 What is the use of these three types of messages? 32 00:01:58,120 --> 00:01:59,840 You can differentiate them. 33 00:02:00,000 --> 00:02:02,480 It changes the way programs are executed. 34 00:02:02,640 --> 00:02:06,800 You always execute what's between parentheses first; 35 00:02:06,960 --> 00:02:09,880 then unary messages; then binary; then keywords. 36 00:02:10,040 --> 00:02:13,760 When at the same level, you use the same order, 37 00:02:13,920 --> 00:02:15,480 from left to right. 38 00:02:17,640 --> 00:02:20,800 Let's see about unary messages: 39 00:02:20,960 --> 00:02:25,000 if I ask small integer 1 what its class is, it'll return SmallInteger. 40 00:02:25,680 --> 00:02:29,040 This will be your homework for next week: 41 00:02:29,200 --> 00:02:33,160 if I send the message not to the Boolean false, I get the Boolean true. 42 00:02:33,720 --> 00:02:36,520 If I query the class Date for today's date, 43 00:02:36,680 --> 00:02:40,480 it'll return 24 May 2009, for instance, which is a Date Object. 44 00:02:40,640 --> 00:02:43,320 If I send the message pi to the class Float, 45 00:02:43,480 --> 00:02:46,240 it'll return a representation of the object pi. 46 00:02:47,640 --> 00:02:49,720 I don't know whether you noticed, 47 00:02:49,880 --> 00:02:54,040 but I sent messages to objects 48 00:02:54,200 --> 00:02:57,520 as well as messages to classes in the exact same way. 49 00:02:57,680 --> 00:03:01,680 I didn't say: "It's a class. Careful, you have to do something." 50 00:03:01,840 --> 00:03:06,560 I send the message class to the small integer, it returns the class. 51 00:03:06,720 --> 00:03:10,880 I send the message today to the class Date, it returns a date. 52 00:03:11,040 --> 00:03:12,240 Nothing fancy. 53 00:03:12,400 --> 00:03:14,760 In Pharo, classes are also objects. 54 00:03:14,920 --> 00:03:19,400 Messages sent to classes are processed like messages to instances. 55 00:03:19,560 --> 00:03:21,320 Which makes complete sense. 56 00:03:22,600 --> 00:03:24,360 I have an example for you. 57 00:03:24,520 --> 00:03:28,000 This exercise isn't related to syntax. It's just to have fun. 58 00:03:28,720 --> 00:03:32,760 I thought it would be fun to determine what are all the methods 59 00:03:34,040 --> 00:03:37,640 implemented in the class Point. It's a unary message. I can say: 60 00:03:37,800 --> 00:03:42,280 "class Point, return all your methods and the message names you understand." 61 00:03:42,440 --> 00:03:45,240 It'll return a long list with all the messages. 62 00:03:45,880 --> 00:03:48,560 See how easily you can build tools? 63 00:03:48,720 --> 00:03:50,840 If I put this in a list, 64 00:03:51,400 --> 00:03:54,920 I'll get a list with all the messages the class returns. 65 00:03:55,080 --> 00:03:57,400 That's how the browser you'll use works. 66 00:03:57,960 --> 00:04:02,160 Let's refine this: I'd like to query the system 67 00:04:02,320 --> 00:04:05,520 for all the unary selectors of the class Point. 68 00:04:05,680 --> 00:04:09,240 I write it like this: "Class Point, list all your selectors." 69 00:04:09,400 --> 00:04:12,560 It'll return an array, a collection. 70 00:04:12,720 --> 00:04:15,120 I write that I want to select... 71 00:04:15,960 --> 00:04:18,080 This is a keyword message. 72 00:04:18,240 --> 00:04:21,960 I want to select among the selectors those which are unary. 73 00:04:22,120 --> 00:04:24,560 There's a query called #isUnary. 74 00:04:24,720 --> 00:04:28,800 It's a message about method names which selects such things. 75 00:04:28,960 --> 00:04:33,280 This is a small footnote in the syntax. 76 00:04:33,440 --> 00:04:37,520 You'll learn about it in the course about iterators, select: being one. 77 00:04:37,680 --> 00:04:40,920 It's very elegant: I can query the system in one line. 78 00:04:41,800 --> 00:04:43,480 Back to the subject at hand. 79 00:04:44,160 --> 00:04:48,120 There are binary messages: they are composed of anObject, 80 00:04:48,280 --> 00:04:50,280 aBinarySelector, anArgument. 81 00:04:50,440 --> 00:04:55,560 Binary selectors are a combination of these incomprehensible things: 82 00:04:55,720 --> 00:04:57,240 %, |... 83 00:04:58,120 --> 00:05:02,960 What can't be understood is binary. What looks like math is binary. 84 00:05:03,880 --> 00:05:06,800 For instance, I send the message + to the object 1 85 00:05:06,960 --> 00:05:10,520 with the object 2 as argument. It returns 3, which is great. 86 00:05:10,680 --> 00:05:14,080 > is a binary message. 87 00:05:14,240 --> 00:05:17,880 @ is also a binary message (it creates points). 88 00:05:18,040 --> 00:05:21,200 String concatenation with a comma is a binary message. 89 00:05:21,360 --> 00:05:23,760 I write: 'Black chocolate','is good' 90 00:05:23,920 --> 00:05:27,360 I get the string 'Black chocolate is good'. Which is true. 91 00:05:27,520 --> 00:05:30,480 About keyword messages... 92 00:05:30,640 --> 00:05:33,520 People often find them frustrating 93 00:05:34,120 --> 00:05:37,800 when they're used to a Java-like or C-like syntax, 94 00:05:37,960 --> 00:05:42,240 with a period, parentheses, and a comma in the middle. 95 00:05:42,400 --> 00:05:44,520 But we do it like this: in the middle. 96 00:05:46,640 --> 00:05:50,040 What you have to see in this example is that the message is: 97 00:05:50,200 --> 00:05:55,360 kayword1keyword2. 98 00:05:58,440 --> 00:06:02,760 The message is full. It's composed of two parts. 99 00:06:05,840 --> 00:06:09,440 It's time to test yourself. I'll give you a couple seconds. 100 00:06:09,600 --> 00:06:15,480 What are these messages? What types do they belong to? 101 00:06:15,640 --> 00:06:18,320 The first has no argument: it must be unary. 102 00:06:18,480 --> 00:06:20,360 The second too. 103 00:06:20,520 --> 00:06:22,960 There's a colon: it must be a keyword. 104 00:06:23,120 --> 00:06:26,840 There's something with a comma I don't understand: it's binary. 105 00:06:27,000 --> 00:06:31,080 It's weird: it's binary. There's no argument: it's unary. 106 00:06:31,240 --> 00:06:33,720 There's a colon: it's a keyword. 107 00:06:33,880 --> 00:06:36,160 It might seem difficult, but... 108 00:06:36,320 --> 00:06:38,200 This is the solution. 109 00:06:38,800 --> 00:06:40,080 For example, 110 00:06:40,240 --> 00:06:43,600 let's imagine I want to change the x value of a point. 111 00:06:43,760 --> 00:06:47,320 This is a point. It's a binary message. 112 00:06:48,000 --> 00:06:50,400 I write setX: with an argument. 113 00:06:50,560 --> 00:06:53,360 It returns a point in which 2 has been changed. 114 00:06:53,920 --> 00:06:55,880 It doesn't look so hard, does it? 115 00:06:56,960 --> 00:07:00,800 Now with the message at:put:. I have an array. 116 00:07:04,040 --> 00:07:08,200 It contains strings: 'Calvin' 'hates' 'Suzie'. 117 00:07:08,840 --> 00:07:12,120 I want to change the second element. 118 00:07:12,280 --> 00:07:15,960 I write at: 2 put: 'loves'. 119 00:07:16,120 --> 00:07:18,760 This is 2. So 'loves' comes here. 120 00:07:18,920 --> 00:07:21,120 About arrays, you can see 121 00:07:21,280 --> 00:07:24,600 the method is called at:put:. 122 00:07:24,760 --> 00:07:28,240 This method changes the value of a collection at a given index. 123 00:07:28,400 --> 00:07:33,160 As you can see, collections start at 1 in Pharo, not at 0. 124 00:07:33,320 --> 00:07:35,920 I want the second element, so I count: 1, 2. 125 00:07:36,080 --> 00:07:38,360 You don't have to subtract anything. 126 00:07:40,520 --> 00:07:46,080 I'll give another example of keyword messages: 127 00:07:46,240 --> 00:07:49,640 when I ask whether 12, which is an integer, 128 00:07:49,800 --> 00:07:51,920 is between 10 and 20, 129 00:07:52,960 --> 00:07:57,680 it's the message between: and:. 130 00:07:58,840 --> 00:08:03,520 In the syntax, it would be the same as: 12.between 131 00:08:05,280 --> 00:08:08,520 And(10,20). 132 00:08:10,400 --> 00:08:13,680 I think I've focused on this enough. 133 00:08:14,560 --> 00:08:16,560 There are three types of messages: 134 00:08:16,720 --> 00:08:19,920 unary, binary, and keywords. 135 00:08:20,960 --> 00:08:22,000 That's all.