Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Dylan (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Methods and generic functions== In Dylan, methods are not intrinsically associated with any specific class; methods can be thought of as existing outside of classes. Like CLOS, Dylan is based on [[multiple dispatch]] (multimethods), where the specific method to be called is chosen based on the types of all its arguments. The method need not be known at compile time, the understanding being that the required function may be available, or not, based on a user's preferences. Under Java the same methods would be isolated in a specific class. To use that functionality the programmer is forced to ''import'' that class and refer to it explicitly to call the method. If that class is unavailable, 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 specific class, and can be called natively by anyone. Linking a specific generic function to a method in a class is accomplished thusly: <syntaxhighlight lang=dylan> define method turn-blue (w :: <window>) w.color := $blue; end method; </syntaxhighlight> This definition is similar to those in other languages, and would likely be encapsulated within the <code><window></code> class. Note the := setter call, which is [[syntactic sugar]] for <code>color-setter($blue, w)</code>. 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 <code>to-string</code>, 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 "<code>to-string</code>", thereby removing this code from the definition of the class itself. If a specific object did not support a <code>to-string</code>, it could be easily added in the <code>to-string</code> module.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)