Dylan (programming Language) - Methods and Generic Functions

Methods and Generic Functions

In Dylan, methods are not intrinsically associated with any particular class; methods can be thought of as existing outside of classes. Like CLOS, Dylan is based on multimethods, where the specific method to be called is chosen based upon the types of all its arguments. The method does not have to be known at compile time, the understanding being that the required functionality may be available or may not, based on the user's preferences.

Under Java the same methods would be isolated in a particular class. In order to use that functionality the programmer is forced to import that class and refer to it explicitly in order to call the method. If that class is not available, or unknown at compile time, the application simply won't compile.

In Dylan, code is isolated from storage in functions. Many classes have methods that call their own functions, thereby looking and feeling like most other OO languages. However code may also be located in generic functions, meaning they are not attached to a particular class, and can be called natively by anyone. Linking a particular generic function to a method in a class is accomplished this way:

define method turn-blue (w :: ) w.color := $blue; end method;

This definition is similar to those in other languages, and would likely be encapsulated within the class. Note the := setter call, which is syntactic sugar for color-setter($blue, w).

The utility of generic methods comes into its own when you consider more "generic" examples. For instance, one common function in most languages is the to-string, which returns some human-readable form for the object. For instance, a window might return its title and its position in parens, while a string would return itself. In Dylan these methods could all be collected into a single module called "to-string", thereby removing this code from the definition of the class itself. If a particular object did not support a to-string, it could be easily added in the to-string module.

Read more about this topic:  Dylan (programming Language)

Famous quotes containing the words methods, generic and/or functions:

    If you want to know the taste of a pear, you must change the pear by eating it yourself.... If you want to know the theory and methods of revolution, you must take part in revolution. All genuine knowledge originates in direct experience.
    Mao Zedong (1893–1976)

    “Mother” has always been a generic term synonymous with love, devotion, and sacrifice. There’s always been something mystical and reverent about them. They’re the Walter Cronkites of the human race . . . infallible, virtuous, without flaws and conceived without original sin, with no room for ambivalence.
    Erma Bombeck (20th century)

    In today’s world parents find themselves at the mercy of a society which imposes pressures and priorities that allow neither time nor place for meaningful activities and relations between children and adults, which downgrade the role of parents and the functions of parenthood, and which prevent the parent from doing things he wants to do as a guide, friend, and companion to his children.
    Urie Bronfenbrenner (b. 1917)