Early documentation

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

Docs

Functions and dispatch

Functions form open families whose overloads are selected using the types of their arguments.

Inheritance and open functions

Lycan's object system takes inspiration from Common Lisp's CLOS. A product declares its fields and can inherit from multiple parents, with functions defined separately to operate on it. Overloads with the same name join an open family and can specialize on the product or its descendants.

An open voice family.lyc ↓
(pub type Animal ())
(pub type Wolf < Animal ())
(pub type Raven < Animal ())

(pub fn voice ((animal &Animal)) -> string
  "...")

(pub fn voice ((wolf &Wolf)) -> string
  "howl")

(pub fn voice ((raven &Raven)) -> string
  "croak")

(pub fn listen ((animal &Animal)) -> string
  (voice animal))
result(listen (Raven)) → "croak"

The < in a type declaration introduces inheritance. A Wolf can be used through an Animal reference, and the family still dispatches on the concrete value.

Multiple arguments

A family can specialize on several parameters. For example, overloads of damage can distinguish a projectile from other entities and a shielded target from an unprotected one.

Dispatch on both arguments.lyc ↓
(pub type Entity
  ((id i32 0)))

(pub type Projectile < Entity
  ((power i32 10)))

(pub type Shielded < Entity
  ((armour i32 7)))

(pub fn damage ((source &Entity) (target &Entity)) -> i32
  0)

(pub fn damage ((source &Projectile) (target &Entity)) -> i32
  [source power])

(pub fn damage ((source &Projectile) (target &Shielded)) -> i32
  (max 0 (- (next-method source target) [target armour])))
result(damage (Projectile) (Shielded)) → 3

An overload can call next-method with its arguments to continue through a less specific implementation. Here, the shielded-target overload subtracts armour from the projectile's usual damage. Applicable overloads are ordered by specificity, comparing parameter positions from left to right.

Protocols

A protocol declares a set of operations that a type satisfies through compatible visible overloads. conform asks the compiler to check that relationship.

A named capability.lyc ↓
(pub protocol Named (Self)
  (fn name ((value &Self)) -> string))

(pub type Project
  ((title string)))

(pub fn name ((project &Project)) -> string
  [project title])

(conform Named Project)

(pub fn greeting (T) (where (Named T)) ((value &T)) -> string
  (+ "Hello, " (name value) "."))
result(greeting (Project :title "Lycan")) → "Hello, Lycan."

The where clause constrains the generic type parameter. At a concrete call, the compiler specializes the function and checks the required operations.

Protocols may also declare associated types. Borrowed protocol views, written &Protocol or !&Protocol, let eligible operations work through a type-erased reference.

Method specifiers

The method specifiers :before and :after attach overloads that run before and after a family's primary implementation. Both return void.

An :around overload controls whether execution continues by calling next-method. It returns the family's result type, so it can also supply a result without running the rest of the call.

Method specifiers.lyc ↓
(pub type Job
  ((enabled bool true)
   (events string "")))

(pub fn execute ((job !&Job)) -> i32
  (set [job events] (+ [job events] "primary "))
  42)

(fn execute :before ((job !&Job)) -> void
  (set [job events] (+ [job events] "before ")))

(fn execute :after ((job !&Job)) -> void
  (set [job events] (+ [job events] "after")))

(fn execute :around ((job !&Job)) -> i32
  (if [job enabled]
    (next-method job)
    0))

Calling execute on an enabled job returns 42 and records before primary after. When the job is disabled, the :around overload returns 0 without running the other methods. These specifiers can also extend a family's behaviour across modules.

Next pageValues and ownership