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!
===Example code=== A simple class with several slots: <syntaxhighlight lang="dylan"> define class <point> (<object>) slot point-x :: <integer>, required-init-keyword: x:; slot point-y :: <integer>, required-init-keyword: y:; end class <point>; </syntaxhighlight> By convention, classes are named with less-than and greater-than signs used as [[angle bracket]]s, e.g. the class named <code><point></code> in the code example. In <code>end class <point></code> both <code>class</code> and <code><point></code> are optional. This is true for all <code>end</code> clauses. For example, you may write <code>end if</code> or just <code>end</code> to terminate an <code>if</code> statement. To make an instance of <code><point></code>: <syntaxhighlight lang="dylan"> make(<point>, x: 100, y: 200) </syntaxhighlight> The same class, rewritten in the most minimal way possible: <syntaxhighlight lang="dylan"> define class <point> (<object>) slot point-x; slot point-y; end; </syntaxhighlight> The slots are now both typed as <code><object></code>. The slots must be initialized manually: <syntaxhighlight lang="dylan"> let p = make(<point>); point-x(p) := 100; // or p.point-x := 100; point-y(p) := 200; // or p.point-y := 200; </syntaxhighlight> By convention, constant names begin with "$": <syntaxhighlight lang="dylan"> define constant $pi :: <double-float> = 3.1415927d0; </syntaxhighlight> A factorial function: <syntaxhighlight lang="dylan"> define function factorial (n :: <integer>) => (n! :: <integer>) case n < 0 => error("Can't take factorial of negative integer: %d\n", n); n = 0 => 1; otherwise => n * factorial(n - 1); end end; </syntaxhighlight> Here, <code>n!</code> and <code><integer></code> are just normal identifiers. There is no explicit [[return statement]]. The result of a method or function is the last expression evaluated. It is a common style to leave off the semicolon after an expression in return position.
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)