6.001 Structure and Interpretation of Computer Programs. Copyright o 2004 by Massachusetts Institute of Technology 6.001 Notes: Section 14. 4 Slide 14.4.1 Cleaning up some details of our implementation So we have seen a first pass at building an object-oriented tem, using Scheme as the base. There are a few details that The need for self-reference we still need to clean up however. These include what to do if a.Dealing withtags'" class does not have a method to handle some request, the need to be able to refer to the object within methods belong to an object (i.e. an ability to recursively use methods of an object within an object), and the need to identify types of objects Detection of methods (or missing methods) Slide 14. 4.2 Use (no-method) to indicate that there is no method What happens if the object doesnt know how to handle a ( define n。- method message? We need a way to detect if we actually have a method (lot ((tag (list No-METHOD))) available To do this, we first need a kind of tagged system that will tell us when we have no method but remem ber our convention anytime we ask an object for something, it should return a procedure. Thus our way of saying no method exists also needs to be a procedure, as shown with no-method, a procedure of no arguments with access to a local frame enclosing a specia Slide 14. 4.3 Detection of methods(or missing methods) Then, to check if we have a method we take an object, such as Use (no-method) to indicate that there is no method might be returned by as k and we do the following: First, is the define n。- method argument a procedure? If it is, we assume it is a method, and we proceed. If it is not, we check to see if the value passed in is a no lambda ( tag))) method. We do this by applying no-method to get out. Check if something is a method the tag, then checking to see if the argument is eg? to it. If it is, (define (nethod? x) then we return false to indicate that no method exists for this eq?x(n。- nethod) object and message. Otherwise, we signal an error. else So why go through all of this? We need to distinguish between (err。r" Obect returned non- mes sage”x))) an error in our implementation and an error in trying to get an instance to handle a message it doesn t know about This lets us separate the details of the implementation from use of the implementation6.001 Structure and Interpretation of Computer Programs. Copyright © 2004 by Massachusetts Institute of Technology. 6.001 Notes: Section 14.4 Slide 14.4.1 So we have seen a first pass at building an object-oriented system, using Scheme as the base. There are a few details that we still need to clean up however. These include what to do if a class does not have a method to handle some request, the need to be able to refer to the object within methods belong to an object (i.e. an ability to recursively use methods of an object within an object), and the need to identify types of objects. Slide 14.4.2 What happens if the object doesn't know how to handle a message? We need a way to detect if we actually have a method available. To do this, we first need a kind of tagged system that will tell us when we have no method. But remember our convention: anytime we ask an object for something, it should return a procedure. Thus our way of saying no method exists also needs to be a procedure, as shown with no-method, a procedure of no arguments with access to a local frame enclosing a special symbol. Slide 14.4.3 Then, to check if we have a method, we take an object, such as might be returned by ask and we do the following: First, is the argument a procedure? If it is, we assume it is a method, and we method. We do this by applying no-method to get out the tag, then checking to see if the argument is eq? to it. If it is, then we return false to indicate that no method exists for this object and message. Otherwise, we signal an error. So why go through all of this? We need to distinguish between an error in our implementation and an error in trying to get an instance to handle a message it doesn't know about. This lets us separate the details of the implementation from use of the implementation. proceed. If it is not, we check to see if the value passed in is a no-