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 men got pregnant, there would be safe, reliable methods of birth control. Theyd be inexpensive, too.”
—Anna Quindlen (b. 1952)
“Mother has always been a generic term synonymous with love, devotion, and sacrifice. Theres always been something mystical and reverent about them. Theyre 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)
“If photography is allowed to stand in for art in some of its functions it will soon supplant or corrupt it completely thanks to the natural support it will find in the stupidity of the multitude. It must return to its real task, which is to be the servant of the sciences and the arts, but the very humble servant, like printing and shorthand which have neither created nor supplanted literature.”
—Charles Baudelaire (18211867)