1.4generic Subroutines and Modules

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Generic Subroutines and Modules

 Generic modules or classes are particularly valuable for creating containers:


data abstractions that hold a collection of objects
 When defining a function, we don't need to give all the types
 When we invoke the class or function we specify the type:
parametric polymorphism
 Generic subroutines (methods) are needed in generic modules (classes), and may also be useful in their own right (see Java
static generic methods).
 Generic programming in programming languages:
 One approach is implicit parametric polymorphism:
 Dynamic typing.  Just try running the code.
 No checking at compile time - not type safe.
 Python approach.
 An alternative is to have a function that has parameterized types: explicit parametric polymorphism
 Generic classes and methods
 Can be static typed checked  Java approach static boolean allEqual(T a, T b, T c) { return a.equals(b) && b.equals(c);
 Implementation approaches:
 C++:
  generates new code for each type:
 linker can help with that
 allows specialization
 can make the code bigger
 can use types in the function: new T();
 Templates can cause horrible error messages
 Java
 type erasure: replace all type parameters in generic types with their bounds
 Only one instance of the code at run time
 Can't do operations involving the type
 Generics are better than macros:
E.g., take the macro:
#define min(a, b) (a < b) ? a : b
Problem: min(a++, b++)
 Variables a++ or b++ evaluated more than once
C++ generic:
template T min(T a, T b)
{ return (a < b ) ? a : b; }
 Far fewer problems: variables evaluated only once.
Exception Types
 What is an exception?
 a hardware-detected run-time error or unusual condition detected by software
 Examples
arithmetic overflow
end-of-file on input
wrong type for input data
user-defined conditions, not necessarily errors
 What is an exception handler?
code executed when exception occurs
may need a different handler for each type of exception
 Why design in exception handling facilities?
allow user to explicitly handle errors in a uniform manner
Event types

 An event is something to which a running program (a process) needs to respond, but which occurs
outside the program, at an unpredictable time.
 The most common events are inputs to a graphical user interface (GUI) system: keystrokes, mouse
motions, button clicks.
 They may also be network operations or other asynchronous I/O activity: the arrival of a message,
the completion of a previously requested disk operation
 A handler—a special subroutine— is invoked when a given event occurs.
 Thread-Based Handlers:
 In modern programming languages and run-time systems, events are often handled by a separate
thread of control, rather than by spontaneous subroutine calls
 With a separate handler thread, input can again be synchronous: the handler thread makes a system
call to request the next event, and waits for it to occur.
 Meanwhile, the main program continues to execute.
 Many contemporary GUI systems are thread-based.
 most use anonymous inner classes for handlers

You might also like