16: Creating Methods

  1. 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.
  2. A method is tied to an operation by following the operation declaration with an equal sign and the method's block.
  3. Local variables are declared at the beginning of the block, followed by statements using those variables.
    1. 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.
      1. If the identified argument is a property, a method doesn't declare a variable for the argument; it simply uses the previously declared property.
    2. 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.)
  4. Variables, properties, private attributes, public attributes: thinking of scope when holding data
    1. 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.
    2. 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.
      1. 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.
    3. An attribute, whether it's public or private, has scope across all methods of a class, and scope across all subclasses of the class
      1. An instance of a subclass can use its subclass-defined features to directly get or set the attribute.
    4. Note: Properties and variables, as internal data, have no passage and don't need passage defined
  5. Throwing an exception within a method
    1. Use the "throw" keyword.
    2. Specify a subclass of Exception.
    3. How an exception propagates up the thread if uncaught.
      1. 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.
      2. The process is killed if the exception isn't caught in the last frame.
  6. Creating a return value for the operation
    1. Use the "Return" keyword.
    2. You can also allow the last statement of the method to set the return value.
  7. Overriding inherited methods
    1. The overriding method must still fit the operation declaration of the overridden method.
  8. Escalating an operation
    1. A superclass may have a different method for an operation that is present in both the class and its subclass.
    2. The escalation symbol (^) asks the currently executing method to escalate execution to a different method belonging to the same operation in a superclass.
    3. The order of escalation depends on canonic search order (describe here).
    4. You can specify escalation to a mix-in (describe how).
    5. 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)
    6. 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.