rev 496 - in trunk: docs/tutorial pr/test

SVN User <[email protected]> Mon, 17 May 2004 21:15:21 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-05-17 21:15:17 -0400 (Mon, 17 May 2004)
New Revision: 496

Modified:
   trunk/docs/tutorial/tutorial8.htm
   trunk/pr/test/test.pr
Log:
started prototype section of tutorial

Modified: trunk/docs/tutorial/tutorial8.htm
===================================================================
--- trunk/docs/tutorial/tutorial8.htm	2004-05-17 19:50:21 UTC (rev 495)
+++ trunk/docs/tutorial/tutorial8.htm	2004-05-18 01:15:17 UTC (rev 496)
@@ -79,17 +79,95 @@
         <p>So now we have an object called &quot;Rectangle&quot; that represents 
           a Rectangle by storing its width and height in attributes called &quot;width&quot; 
           and &quot;height&quot; and can calculate its own area by it's &quot;area&quot; 
-          method that is also strored in an attribute called &quot;area&quot;. 
+          method that is also stored in an attribute called &quot;area&quot;. 
         </p>
         <p>Those of you familiar with object-oriented programming might notice 
           that this is very similar behaviour to an object instance from a class 
-          that might be called &quot;Rectangle&quot;, yet we have not defined 
-          any class at all, we just created the object directly. This is much 
-          simpler and more direct. This is called &quot;object-centric&quot; programming 
-          instead of &quot;object-oriented&quot; programming even though it is 
-          obviously &quot;oriented&quot; towards objects. It is just that all 
-          the books and courses have already been written that say that object-oriented 
-          programming has classes and it's a bit too late too argue with them.</p>
+          called &quot;Rectangle&quot;, yet we have not defined any class at all, 
+          we just created the object directly. This is much simpler and more direct. 
+          This is called &quot;object-centric&quot; programming instead of &quot;object-oriented&quot; 
+          programming even though it is obviously &quot;oriented&quot; towards 
+          objects. It is just that all the books and courses have already been 
+          written that say that object-oriented programming has classes and it's 
+          a bit too late too argue with them.</p>
+        <p>At this point we might want a lot of Rectangles. We can always make 
+          a second one by copying it. This is how some languages, and the Self 
+          language in particular, makes more of a &quot;type&quot; of object:</p>
+        <pre>>>> rect2 = Rectangle.copy()
+>>> print rect2.area()
+100
+>>> rect2.width = 50
+50
+>>> print rect2.area()
+500
+>>> print Rectangle.area()
+100
+>>></pre>
+        <p>So you can see that we now have two objects each with different width 
+          attributes and different areas that represent different rectangles. 
+          At this point there is nothing stopping us from using a for loop to 
+          make a thousand rectangles. </p>
+        <p>So we can make many instances of rectangles that are just like the 
+          first Rectangle. That is why we call the first one the prototype. It 
+          is not like the class in an object-oriented language. A class is just 
+          a blueprint for making objects. A prototype is a real, working instance 
+          of an object. You can make the prototype and test it before you use 
+          it to make other instances. Using prototypes instead of classes is what 
+          makes Prothon a prototype-based language instead of a class-based language.</p>
+        <p>So did I trick you and let you think you were just making an object 
+          when you were really making a prototype? No, in reality every object 
+          in Prothon can also be a prototype. You could copy &quot;rect2&quot; 
+          to make a &quot;rect3&quot; and use it just like you copied &quot;Rectangle&quot;. 
+          Later you will find out things that make certain objects better at being 
+          prototypes than other objects, but Prothon is an equal opportunity employer. 
+          Any object may apply to be a Prototype.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="oaok"></a>6.2 
+          Parent Prototype Links</font></p>
+        <p>You may have noticed that the instances created above by the copy commands 
+          above have some disadvantages compared to object-oriented intances made 
+          by classes. The biggest problem is storage efficiency. Each of one the 
+          rectangle instance objects has its very own copy of the area method. 
+          Also, what if these objects are stored away somewhere and we decide 
+          we want to make an improvement to the Prothon code for the area method? 
+          It would be nice to change the code in one place and have it change 
+          for all rectangles. It would not be fun to change the code in a thousand 
+          different area methods.</p>
+        <p>To solve that problem and to add many new features, Prothon has added 
+          a third feature to objects, after binary data and attributes. This is 
+          an ordered list of links to prototypes. Each object has at least one 
+          reference (link) to an object that is its &quot;parent prototype&quot;. 
+          There can be more than one but let's discuss the case of one for right 
+          now.</p>
+        <p>The parent prototype is just an ordinary object. The parent prototype 
+          is used whenever the object is having an attribute looked up and the 
+          attribute is not found. In this case, the search for that attribute 
+          continues in the parent's attributes. If the attribute isn't found there, 
+          then it continues in the parent's parent (grandparent's) attributes. 
+          In other words, search for attributes goes up the chain of prototype 
+          objects linked together by the prototype links.</p>
+        <p>You might wonder what ends this prototype chain. There is a special 
+          object, cleverly named &quot;Object&quot;, that is the end of all prototype 
+          chains. It is the ultimate parent of all objects. The common way to 
+          create an empty object is to give it one prototype link to Object. That 
+          is what we did earlier when we said &quot;Rectangle = Object()&quot;. 
+          (We will explain this statement in the next paragraph). To show this, 
+          will use the function protoList() which returns the prototype chain 
+          for an object as a list. It includes Rectangle as the head of the list 
+          and Object as the end:</p>
+        <pre>>>> print Rectangle.protoList()
+[&lt;Object:93b100&gt;, &lt;Object:923020:Object: prototype base for all objects&gt;]
+>>></pre>
+        <p>So what good is attribute lookup in prototypes? Let's redo rect2 using 
+          a prototype link instead of doing a copy. The easiest way to create 
+          an object that has a prototype link to another object is to use the 
+          form &quot;obj = Prototype()&quot;. This looks exactly like a function 
+          call, but we know it isn't, because we know &quot;Prototype&quot; is 
+          not a function object. This will create a new object that has no binary 
+          data or attributes but it will have one prototype link to the object 
+          &quot;Prototype&quot;. Now you can see what our original statement &quot;Rectangle 
+          = Object()&quot; was doing. It created an empty object with only a prototype 
+          link to Object.</p>
+        <p>&nbsp;</p>
         </td>
     </tr>
   </table>
Modified: trunk/pr/test/test.pr
===================================================================
--- trunk/pr/test/test.pr	2004-05-17 19:50:21 UTC (rev 495)
+++ trunk/pr/test/test.pr	2004-05-18 01:15:17 UTC (rev 496)
@@ -1,35 +1,10 @@
 
-def err(n):
-	print 'test',n,'failed'
-	Sys.exit(1)
 
-x = "abcdefghijklm"
-
-if "c:bc:acegi:mlkjih" !=  x[2]+':'+x[1:3]+':'+x[0:10:2]+':'+x[12:6:-1]:
-	err(1)
-
-x = "abcdef"
-
-if "e:de:def:ab:fed:fedcba:abcdef" !=  x[-2]+':'+x[-3:-1]+':'+x[3:]+':'+x[:2]+':'+x[:2:-1]+':'+x[::-1]+':'+x[:]:
-	err(2)
-
-if 3 !=  Len("abc"):  err(3)
-
-print "abc".upper()
-
-print "ABC".lower()
-
-if "Abc def" !=  "abc def".capitalize(): err(6)
-
-if "Abc Def" !=  "abc def".capWords(): err(7)
-
-if "<x >" != '<'+" x ".lStrip()+'>': err(8)
-
-if "< x>" !=  '<'+" x ".rStrip()+'>': err(9)
-
-if "<x>" !=  '<'+" x ".strip()+'>': err(10)
-
-print '\nall tests passed\n'
-
-
-
+Rectangle = Object()
+with Rectangle:
+    width = 10
+    height = 10
+    def area():
+        return self.width * self.height
+        
+rect2 = Rectangle()