Reflective Facilities in Smalltalk

Reflective Facilities in Smalltalk

Reflective Facilities in Smalltalk- Brian Foote Ralph E. Johnson Dept. of Computer Science University of Illinois at Urbana-Champaign 1304 W. Springfield Urbana, IL 61801 [email protected] (217) 333-3411 [email protected] (217) X4-0093 Abstract Monitors, atomic objects, and distributed objects present similar difficulties. These applications all require that an Computational reflection makes it easy to solve problems object be able to intercept any messagesent to it on a per- that are otherwise difficult to address in Smalltalk-80, such object rather than a per method-basis. In Smalltalk-80, the as the construction of monitors, distributed objects, and method dispatch mechanism is buried within the Virtual futures, and can allow experimentation with new Machine, beyond the programmer’s grasp. inheritance, delegation, and protection schemes. FulI reflection is expensive to implement. However, the ability Recent work on object-oriented computational reflection to override method lookup can bring much of the power of /$faes 1987h] has provided a general framework for reflection to languages like Smalltalk- at IXJ cost in addressing problems like those discussed above. Reflection efficiency. is intriguing because it seemsto allow programmers to make open-ended,localized extensions to the languagesand Introduction systems with which they are working. Reflective systems are f?equently constructed using One of the attractions of object-oriented languages is their metacircular interpreters, This approach provides the extensibility. Programmers gain confidence that facilities highest possible degree of flexibility at the expense of that are not already available in a given environment can performance, We believe that full reflection may not be easily be constructed. Current systems make it easy to necessary to addressmany of the kinds of problems that define data types like complex or arbitrary precision have attracted so much attention to it. For this reason, we numbers. New object types become as “first class” as those have focused on how a handful of reflective facilities can built into the language. A big part of the appeal of object- efficientlv bring much of the Rower of full reflection to oriented programming is that programmers believe that, if Smalltalk-80. In particular, this paper discussesredefining they really wanted to, they could redefine the world. the default method lookup mechanism, and looks at how lightweight classescan be used to implement metaobjects. However, there are a few kinds of objects that are difficult to implement in conventional object-oriented programming languages such as Smalltalk- [Goldberg 19831and C++ Reflection [Stroustrup 19861. For example, a future object [Halstead 19851 acts as a surrogate for a result that is in the process A reflective program is one that reasons about itself. A of being computed. Messagesto the future cause the sender fully reflective procedural architecture [Smith 19831is one to wait until the result is computed, at which time the in which a process can accessand manipulate a full, messageis relayed to the result. Object-oriented languages explicit, causally connected representation of its own state. make it easy to redefine simple methods, but futures must “Causally connected” means that any changes made to a intercept any messagesent them. process’sself-representation are immediately reflected in its actual state and behavior. Permission to copy without fee all or part of this material is granted provided waes 1987a] [Maes 1987b] describes an object-oriented that the copies are not made or distributed for direct commercial advantage, reflective language called 3XRS. 3-KRS partitions every the ACM copyright notice and the title of the publication and its date appear, object into a domain specific referent and a reflective and notice is given that copying is by permission of the Association for m&object. The referent contains information describing Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. the real-world entity that the object represents. The 0 1989 ACM 089791-333-7/89/0010/0327 $1.50 metaobject contains information describing its referent as a October l-6, 1989 OOPSLA ‘89 Proceedings 327 computational entity in itself. Enforcing this separation Lisp Based Systems encouragesthe emergenceof standardprotocols for communicating between objects and metaobjects. The The Common Lisp Object System (CLOS) [Bobrow 19881 distribution of the system’s self-representation among the [Keene 19891is a marriage of Common Loops and New system’s metaobjects makes this self-representation easier Flavors that provides a very powerful generic function-based to think about and manipulate. method combination mechanism. The CLOS Metaobject Protocol provides a metacircular definition of the entire Manipulation of an object’s metaobject can affect the way CLOS system. The MOP is intended to permit open-ended in which computation for its referent object is carried out. experimentation with new object-oriented programming For instance, in 3-KRS, an object can have method lookup paradigms, and has been used to successfully implement a redefined by giving it a new metaobject that defined a number of distinctly un-CLOS-like object-oriented “deviating” interpreter. Such changes can be made on a per- programming systems. The MOP is designed to permit the object basis, since each object has its own metaobject. modification of basic language mechanisms such as method Specialized metaobjects can be introduced dynamically. The lookup from with CLOS itself. ability to introduce temporary, localized changes to an object’s semantics without changing the object itself The mechanisms provided in CLOS’s standard method accounts for a great deal of reflection’s power. combination scheme are quite powerful. CLOS generic functions can specialize on any of their arguments, not just Maes distinguishes between systems with reflective the first, and may specialize on instances as well as classes facilities and full-fledged reflective architectures. and types. The :before, :after, and :around method Reflective facilities allow a program to reflect upon certain types modify particular primary methods. Class-wide aspects of itself. For example, in Lisp, eval permits before, after and around methods for arbitrary messages reflecting upon programs given as data, and catch and cannot be defmed without making alterations at the meta- throw allow reflecting upon the runtime stack. level. Actor Languages Co&e’s ObjVLisp system [Cointe 19871 permits a Me&lass hierarchy distinct from the class hierarchy. This An Actor [Agha] is a process that explicitly decides whether allows arbitrarily deep meta-regress,where appropriate. and how to handle or delegate any messagesit receives. This system allows the lookup method used by the This ability is used to construct futures, monitors, etc. system’s send primitive to be changed by manipulating the Actor languages are designed for multiprocessors; the metaclass. Metaclass customization can also be used to requirement that each actor be a separateprocess makes control internal object representations, method caching, and actors inefficient on single processors. Further, using a accessto instance variables. parallel programming language for sequential programming causesa certain mental overhead. Reflecting on the Smalltalk Virtual Smalltalk- [Goldberg 19761 was similar in some respects Machine to the Actor languages. In Smalltalk-72, an object’s class was a process that repeatedly examined the object’s message Most of the existing object-oriented systems that permit stream and then dispatched on the message. As the meta-level system manipulation, such as 34X3, CLOS, language evolved, messagelookup was subsumed into the ObjVLisp, and ABCL/R Watanabe 19881have been Smalltalk Virtual Machine for efficiency’s sake and because constructed using Lisp-based metacimular interpreters. deviations from case-style method dispatching were rare and Reflection is then implemented by modifying the usualIy hard to understand. Smalltalk- permitted more language’s interpreter. Smalhalk-80, on the other hand, reflection than Smalltalk- because objects could uses a virtual machine. manipulate the messagestream. Although the Smalltalk-80 virtual machine is a byte code Replacing user-defmedmessage-handling with standardized interpreter that is usually implemented in machine language method lookup was a success. It led to efficient Smalltalk or C, the official definition of the virtual machine is written implementations and a large body of reusable software. in Smalltalk[Goldberg 831. Moreover, the SmaIltallc-80 However, the power of the Actor language is needed to deal debugger uses a byte-code interpreter written in Smalltalk. with parallelism and other aspects of modem applications. Indeed, debugging and tracing are frequently cited as We believe that object-oriented systems should provide applications that are well addressedusing reflection. In primitive facilities that allow actor-like objects to be general, though, secondary interpeters are probably too constructed out of more rudimentary components. Thus, inefficient to serve as a practical mechanism for the the full power of actors will only be used when appropriate. implementation of reflective features. Hence, we have focused instead on adding certain reflective facilities to the Smalltalk virtual machine. 328 OOPSLA‘89 Proceedings October 1-6, 1989 exploited Smalltalk-80’s doesNotUnderstand:

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    9 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us