16: Creating Methods
- A method is defined in a block that uses the syntax described in the last section of this book. A method block may contain other blocks such as if/then blocks.
- A method is tied to an operation by following the operation declaration with an equal sign and the method's block.
- Local variables are declared at the beginning of the block, followed by statements using those variables.
- A method normally declares a variable for each identified argument in the operation signature. These variables need not be assigned; they're automatically assigned the value of the argument.
- If the identified argument is a property, a method doesn't declare a variable for the argument; it simply uses the previously declared property.
- If the operation signature ends in variable arguments (...), the variable arguments are passed to the method as a list object. The type of list is List[T, C] where T is the type specfied in the signature for the variable arguments and C is the comparator specified. (The default comparator, if it's not specified, is Equal.)
- Variables, properties, private attributes, public attributes: thinking of scope when holding data
- A variable has scope no greater than a method. If you assign a value to a variable in one operation, you can't get that value in another operation.
- A property has scope across all methods of a class. One method can assign a value to a property, another method of the class can get that value.
- A subclass will inherit the property, but has no direct access to it through its own features, only through the inherited features of the class that defined the property. The subclass features have no knowledge of the property.
- An attribute, whether it's public or private, has scope across all methods of a class, and scope across all subclasses of the class
- An instance of a subclass can use its subclass-defined features to directly get or set the attribute.
- Note: Properties and variables, as internal data, have no passage and don't need passage defined
- Throwing an exception within a method
- Use the "throw" keyword.
- Specify a subclass of Exception.
- How an exception propagates up the thread if uncaught.
- If the exception is uncaught by the current frame (belonging to the current method), it's passed up to the next frame, whose method may or may not catch it. If uncaught, it passes up to the next frame, and keeps going until caught or until the last frame is passed.
- The process is killed if the exception isn't caught in the last frame.
- Creating a return value for the operation
- Use the "Return" keyword.
- You can also allow the last statement of the method to set the return value.
- Overriding inherited methods
- The overriding method must still fit the operation declaration of the overridden method.
- Escalating an operation
- A superclass may have a different method for an operation that is present in both the class and its subclass.
- The escalation symbol (^) asks the currently executing method to escalate execution to a different method belonging to the same operation in a superclass.
- The order of escalation depends on canonic search order (describe here).
- You can specify escalation to a mix-in (describe how).
- Compare escalation to casting an operation: escalation occurs w/in a method & uses canonic search order, casting is used w/ an operation to specify the method of a specific superclass (no searching)
- You can specify new args on escalation, or you can depend on the contents of the stack to be escalated. (If stack has been discussed at this point.)
TS Ref - 26 JUN 1996
Generated by the sweat of Mike Boom's brow.