Hello World in Dylan
Your first Dylan program - the classic Hello World example with Docker setup
Every programming journey starts with Hello World. Let’s write our first Dylan program and experience this unique language that combines dynamic flexibility with efficient compilation.
The Code
Dylan programs require a project structure with a library definition file and a source file. Our Docker setup handles this automatically, but let’s understand the pieces.
Create a file named hello.dylan:
| |
This simple source file contains our Hello World code. The Module: header tells Dylan which module this code belongs to.
Understanding the Code
Let’s break down this program:
Module: hello- A header that specifies which module this source file belongs to. In Dylan, code is organized into modules within libraries.format-out- A function from theformat-outmodule that outputs formatted text to standard output, similar toprintfin C."Hello, World!\n"- A string literal with a newline character. Dylan strings use double quotes.
Why the Module Header?
Dylan uses a sophisticated module system inherited from its Lisp heritage. Every source file must declare which module it belongs to. Modules provide namespacing and control what names are visible to other code.
Running with Docker
The easiest way to run this without installing Open Dylan locally:
| |
Our Docker image includes Open Dylan and automatically handles the library and module definitions when it detects a hello.dylan file.
How the Build Works
When you run the Docker command, it:
- Detects
hello.dylanin the mounted/appdirectory - Creates a Dylan project structure with library and module definitions
- Compiles the project using
dylan-compiler - Runs the resulting binary
Expected Output
Hello, World!
Understanding Dylan Project Structure
A complete Dylan project typically includes several files:
Library Definition (library.dylan)
| |
This defines:
- A library named
hellothat uses thecommon-dylanandiolibraries - A module named
hellothat uses thecommon-dylanandformat-outmodules
Source File (hello.dylan)
The source file we created earlier contains the actual program code.
Build Configuration (dylan-package.json)
Modern Open Dylan uses a JSON-based package configuration.
Key Concepts
Libraries and Modules - Dylan code is organized into libraries (compilation units) containing modules (namespaces). This provides strong encapsulation.
format-out - The standard way to output text. It’s part of the
iolibrary and uses format strings similar to printf.No Main Function - Top-level expressions in a module are executed when the program starts.
Object-Oriented - Everything in Dylan is an object, including numbers and functions.
Alternative: Using format
You can also use the more powerful format function for formatted output:
| |
Or with format directives:
| |
Common format directives:
%s- String (usesprint-object)%d- Decimal integer%=- Dylan print representation%%- Literal percent sign
Dylan vs Other Languages
Java:
| |
Python:
| |
Common Lisp:
| |
Dylan:
| |
Notice how Dylan’s syntax resembles Python or Pascal more than its Lisp ancestors, despite sharing many concepts with Common Lisp.
Variables and Types
Dylan supports both untyped and typed variable declarations:
| |
Type names in Dylan are surrounded by angle brackets: <integer>, <string>, <float>.
Functions in Dylan
Here’s how you’d define a simple function:
| |
define methodcreates a method (Dylan’s version of functions)=> ()indicates no return value- The
end method;closes the definition
Dylan’s Multiple Dispatch
One of Dylan’s most powerful features is multiple dispatch:
| |
The appropriate method is chosen based on the runtime type of all arguments.
About Open Dylan
Our Docker image uses Open Dylan, the open-source Dylan implementation that combines:
- Gwydion Dylan - CMU’s open-source compiler
- Functional Developer - Harlequin’s commercial implementation
Open Dylan provides:
- Native code compilation
- Cross-platform support (Linux, macOS, Windows)
- Modern tooling with the
deftpackage manager - Active community development
Next Steps
Continue exploring Dylan’s unique features:
- Classes and the object system
- Multiple dispatch in depth
- The module and library system
- Macros and metaprogramming
- Generic functions and method combinations
Dylan rewards exploration. Its combination of dynamic language flexibility with static language performance offers a unique programming experience.
Welcome to Dylan!
Running Today
All examples can be run using Docker:
docker pull codearchaeology/dylan:latest