Early documentation

The tour and docs will likely be rewritten for Lycan's first public release.

Docs

Macros and compile time

Macros transform source forms as data before ordinary type checking begins.

Macro definitions

A macro receives unevaluated source forms and returns a new form, with an implicit return type. Backquote constructs that form, and a comma inserts a value into it.

Evaluate once, use twice.lyc ↓
(pub macro twice (expression)
  (let ((value (gensym "value")))
    `(let ((,value ,expression))
       (+ ,value ,value))))

(pub fn answer () -> i32
  (twice (+ 20 1)))
result(answer) → 42

The macro evaluates its argument once and uses the result twice, with gensym giving the temporary binding a distinct name. Use ,@ to splice a list of forms into a larger form.

Names and expansion

Introduced identifiers resolve in the macro's defining module, and introduced local bindings are renamed to avoid capturing caller expressions. capture-ident is available when a macro intentionally refers to a caller binding.

A caller's local binding.lyc ↓
(import (examples/macros (twice)))

(pub fn example () -> i32
  (let ((value 21))
    (twice value)))
result(example) → 42

The value passed to twice still refers to the caller's binding; the macro's generated temporary has its own name.

Expansion runs in two rounds so that a macro can reflect on a type another macro declared. Dependencies that would need a third round are rejected with a diagnostic.

HTML

The standard HTML macro turns a tree of element forms into an ordinary Html value. Attributes precede children, and ordinary expressions can produce child values.

A page fragment.lyc ↓
(import (std/html *))

(pub fn greeting ((name string)) -> Html
  (html
    (article :class "greeting"
      (h1 "Hello, " name ".")
      (p "This page is a Lycan value."))))

(to #string (greeting "reader")) renders the tree to HTML with text and attribute values escaped. Functions can build individual sections and compose them into a complete page.

The same tree can be mounted in a browser with std/web. Rendering to a string produces markup; browser event subscriptions are established when building the live DOM.

Compile-time execution

pre-compile blocks run before macro expansion, where they can define helpers and emit declarations. post-compile blocks run after expansion and can consume collected values or produce files.

Generating a function.lyc ↓
(pre-compile
  (emit `(pub fn seconds-per-day () -> i32
           ,(SInt (* 24 60 60)))))
result(seconds-per-day) → 86400

The multiplication runs at compile time. SInt turns the result into an integer source form, and emit adds the generated function to the module. The compiled function simply returns 86400.

Reflection makes type fields, function signatures, and annotations available to derivations. Standard library macros use it to generate serialization code and other repeated structure.

Use functions for computation and macros when a task calls for transforming source structure.

Keep readingReturn to the tour