Prototyp basd languags Author Tomas Billborn & Mallla Srinivasa Rao
Abstract Whn objct orintd languags ar brought up as subjct most of us think of languags that support data abstraction by providing data tmplats calld classs. This sinc class-basd languags ar mor common and familiar to most programmrs than prototyp-basd ons. It may sm a bit pculiar to talk about objcts without classs, but that is th subjct of th papr. Th papr will giv an ovrviw dscription of how prototyp basd languags function and xamin som of thir advantags and disadvantags.
Contnt Introduction...4 Th objct orintatd hirarchy...5 Prototyp basd languags...5 Normal objcts...5 Prototyps...5 Traits...5 Cration of objcts... 5 Ex-nihilo...5 Cloning...5 Shallow cloning...5 Dp cloning...6 Extnsion... 6 Inhritanc...6 Dlgation-basd inhritanc...6 Embddd--basd inhritanc...6 Sharing achivd by shallow cloning...6 Sharing achivd by dlgation...6 Implicit dlgation...6 Explicit dlgation...6 Class-lik sharing in prototyp basd languags... 6 Dynamic inhritanc...7 Advantags of prototyp basd inhritanc... 7 Th nd for abstraction...7 Th origin of prototyp basd languags...7 Conclusion...8 Rfrncs...9
Introduction Sinc th lat 1980s prototyp basd languags and programming has bn proposd as an altrnativ to th class-basd approach of objct orintation programming. Prototyp basd languags ar mainly charactrizd by th fact thy hav rjctd th notation of classs. Instad ach objct holds its own dscription and th lack of bound to a class allows ach objct is allowd to volv on its own. Th most important ida of th prototyp-basd is that concrt objcts ar only man to modl applications. Prototyps ar not mant to b abstractions lik classs and ar not linkd in any othr way to othr objcts that would dscrib thm as in th class-basd approach.
Th objct orintatd hirarchy Th prototyp basd languags ar a sub-sub branch of th objct orintatd hirarchy. 1 Figur 1: A Hirarchy of Objct Orintd Faturs. Prototyp basd languags In a prototyp basd languag thr ar no distinction btwn class and instanc, thr ar simply objcts. Thr ar thr typs of objcts, normal objcts, prototyps and traits. Although th last two ar not tru objcts. Normal objcts: intndd only to b usd and to carry local stat and should rly on traits for thir mthods [1]. Prototyps: a spcial kind of objct that ar intndd to b bluprints from which normal objcts (and prototyps) ar gnratd via clond. Prototyps act lik classs, but thy ar fully functional objcts [1,2,3]. Traits: abstract dscriptions of objcts. Thy ar similar to classs, but ar not allowd to possss stat. A trait provids a st of mthods that implmnts bhaviour. Traits ar intndd only as th shard parnts of normal objcts: and should not b usd dirctly or clond [1,3,4]. Cration of objcts Thr ar thr primitiv ways to crat objcts in prototyp basd languags: Ex-nihilo: cration of objcts with no parnts. If x-nihilo cration is allowd, thy can com in two forms. As mpty nw objcts, or nw objcts with initial bhaviour [2, 11]. Cloning: nw objcts ar cratd by making copis of objcts that alrady xists. Thr ar two typs of cloning. o Shallow cloning: taks a copy of th stat of th objct and shars th bhaviour with th objct from which it was clond by mans of dlgation [5]. 1 All functions mntiond blow may not xist in all prototyp basd languags, but at last on from ach catgory do.
o Dp cloning: taks a copy of both th stat and th bhaviour of th objct [5]. Extnsion: to crat an objct that shars knowldg with a prototyp, you construct an xtnsion objct, which has a list containing its prototyps, which may b shard by othr objcts, and prsonal bhaviour idiosyncratic to th objct itslf [2]. Inhritanc In prototyp basd languags thr ar four catgoris of inhritanc. Dlgation-basd inhritanc: rusing mthods by sharing thm with othr objcts. This can b don implicitly or xplicitly. Embddd-basd inhritanc: rusing mthods by xtracting thm from othr objcts. This can b don implicitly or xplicitly. Sharing achivd by shallow cloning Whn an objct is clond by using shallow cloning th nwly clond objct rcivs that objcts stat and bhaviour [2]. Sharing achivd by dlgation Th basic ida of dlgation is to forward rqusts that cannot b handld by an objct to th prototyp which it is basd on. As long as an objct and its parnt xist dlgation achivs lif-tim sharing. Thr ar two altrnativs of dlgation, implicit and xplicit [2]. Implicit dlgation: whn an objct cannot answr a mssag, th intrprtr automatically dlgats it to anothr objct; objcts hav a parnt link to indicat to th intrprtr to which objcts mssags should b dlgatd [2]. Explicit dlgation: th dlgation of mssags is don xplicitly for ach mssag to b dlgatd; th dlgating objct nams th objct to which th mssag has to b dlgatd [2]. Class-lik sharing in prototyp basd languags As shown cloning and dlgation achiv diffrnt kinds of usful sharing, but is it th sam kind of sharing found in traditional class-basd objct orintatd languags? Th answr to this is no, sinc rus from prototypical instancs using both cloning and dlgation caus smantic problms. Th problm with cloning is that whn modifying th prototypical instanc, thr is no way to insur that th family of objcts clond from this prototypical instanc will b homognous; objcts clond bfor th modification will not b affctd, thus thy will divrg from th nw concpt implmntd by th updatd prototypical instanc. With dlgation, th problm is sn th othr way around. Whn th prototypical instanc is modifid, all objcts dlgating to it ar affctd by th modification; if th prototypical instanc looss its status of bing prototypical and instad bcoms an xcptional instanc of th concpt, th original concpt is lost.
In both cass, a solution may b to mak prototypical instancs immutabl, but this solution is contrivd in two ways. First, it introducs a distinct kind of objcts in th prototyp-basd modl, which thn looss part of its lganc and simplicity. Scond, by making prototypical instancs immutabl, w loos th capability of normally spaking about ths objcts. W conclud that prototypical instancs ar not th right dvic to implmnt [2]. Dynamic inhritanc Som prototyp basd languags lik SELF also supports dynamic inhritanc, th ability of an objct to chang th cod that it inhrits at run tim [6]. Advantags of prototyp basd inhritanc In prototyp-basd inhritanc thr ar two big advantags, simplicity and corrctnss. Simpl sinc th concpt of class is liminatd and rathr thn having two inhritanc rlations as in class-basd languags thr is just on. This rducs th cognitiv load of th programmr. Its mor concrt sinc programming on modifis th objcts dirctly and not class indirctly to achiv th ffct on an objct [7]. Th nd for abstraction Dspit all th advantags of th prototyp basd languags thy still ar facd with two major problms. On is rlatd to th lack of strong idntity for prototyps and th othr on to organisation of programs. Th programming modl of prototyp basd languags ar simplr and mor flxibl, but whn facd with largr programs th lack of satisfactory way to dfin concpts maks it slf visibl. Our othr problm is rlatd to how svral prototyp basd systm try to tackl th problm of organisation of programs with class lik constructs. Som of ths hav bn intgratd to th systm krnl and ar not accssibl to th programmr and thrfor not much hlpful to xplicitly dfin abstractions [8]. Th origin of prototyp basd languags Th dfinit origin of prototyp basd languags is hard to dtrmin, but th crators may bn inspird by Wittgnstin s criticism of classification [3,9] or by th prototyp thory [10] by Elanor Rosch from th mid svntis. Eithr way th paprs rfrrd to abov might not hold th answr to what xactly that is, but will prsnt th radr with som intrsting philosophical background on prototyps on lss tchnical grounds.
Conclusion W hav lookd at a gnral dscription of th prototyp basd languags and com to th conclusion that: Th main charactristic of prototyp basd languags is th fact that thy ar classlss. Thr ar svral diffrnt prototypd languags and all do not posss all th sam functionality, but thy do posss at last on of th diffrnt typs of dscriptions of typ of objcts, cration of objcts, inhritanc that has bn shown. It has also bn shown that prototyp basd languags possss svral advantags ovr class-basd ons. Prototyp basd languags ar concptually asir to undrstand thn classbasd languags, sinc thr is just on typ of objct. Thr is only on kind of rlationship, inhritanc from parnt, not as in for xampl class-basd languags instanc and subclass. Prototyp basd languags ar mor concrt sinc copying and modification of objcts ar mor dirct. Prototyp basd languags ar mor flxibl sinc it can hold bhaviour or, through copy act as a sourc for nw objcts. With th advantags also coms som disadvantags. Prototyps can b inadvrtntly modifid which might affct futur clons. This can lad to subtl bugs. This is rfrrd to as th prototyp corruption problm. Th construction of crtain objcts rquirs a crtain construction plan to b xcutd. In Class Basd Languags, it is possibl to formalis this plan as a class constructor. In Prototyp basd languags thr is no such thing: objcts ar cratd by cloning othr objcts and it rquirs a lot of disciplin from programmrs to mak sur crtain procdurs ar followd. Prototyp basd languags suffr from a r-ntrancy problm. A problm with naiv prototyp-basd thinking is that, whn cloning objcts not only thir stat but also thir bhaviour gts copid. This mans that an objct with 10 mthods will giv ris to 50 mthods whn clond 5 tims, or at last 50 pointrs all pointing to th sam mthods. Th problm can b circumvntd using th traits tchniqu, but this is somtims problmatic as th traits tchniqu havily intrfrs with th inhritanc hirarchy.
Rfrncs 1. [Cardlli96] Luca Cardlli, Objct-basd vs. Class-basd Languags http://rsarch.microsoft.com/usrs/luca/slids/1996-05%20class-basd%20vs% 20Objct-basd%20Languags%20(PLDI%20Tutorial).pdf 2. [DMC92] Christoph Dny, Jacqus Malnfant and Pirr Coint, Prototyp- Basd Languags: From a Nw Taxonomy to Constructiv Proposals and Thir Validation http://portal.acm.org/citation.cfm?id=141954&coll=guide&dl=guide&cfid=441597 98&CFTOKEN=15280960&rt=1#Fulltxt 3. [Psonn01] Juha Pttri Psonn, Psychological criticism of th prototyp-basd objct-orintatd languags http://www.hlsinki.fi/~jppson/paprs/kandi.html 4. [Mrizzi05] Nicholas Mrizzi, Objct Orintd Classs, Objcts, Inhritanc and Typing http://www.cas.mcmastr.ca/~cartt/cas706/2005/ooprsntation.pdf 5. [DM02] Jssi Ddckr and Wolfgang D Mutr, Using th Prototyp-basd Programming Paradigm for Structuring Mobil Applications http://prog.vub.ac.b/publications/2002/vub-prog-tr-02-28.pdf 6. [BAB04] Andi Bjlri, Jonathan Aldrich, and Kvin Birhoff, A typ chckd prototyp-basd modl with linarity http://rports-archiv.adm.cs.cmu.du/anon/isri2004/cmu-isri-04-142.pdf 7. [Smith95] Waltr R. Smith, Using a Prototyp-basd Languag for Usr Intrfac: Th Nwton Projct s Exprinc http://wsmith.bst.vwh.nt/oopsla95.pdf 8. [Bardou00] Danil Bardou, Inhritanc Hirarchy Automatic (R)organization and Prototyp-Basd Languags ftp://ftp.inrialps.fr/pub/romans/publications/bardou00a.pdf 9. [Arts01] Kris Arts, Visto: A Dclarativ Mthodology for Graphical Usr Intrfacs, basd on Haskll http://www.cs.kuluvn.ac.b/publicatis/doctoratn/cw/cw2001_1.pdf 10. [Psonn02] Juha Pttri Psonn, Concpts and Objct-Orintd Knowldg Rprsntation http://thsis.hlsinki.fi/julkaisut/hum/psyko/pg/psonn/concpts.pdf 11. [Wuyts05] Rol Wuyts, Programming Languags http://www.ulb.ac.b/di/rwuyts/info020_2004/09-prototypsandslf.pdf