Early documentation

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

Docs

Values and ownership

The type of a value describes how it relates to its storage and to the code that uses it.

Values and copies

Scalars, products, and sums are ordinary values. Products can contain collections, references, and owned handles as well as other values, with those fields determining how the whole product is copied or moved.

Implicit copies take constant time: strings and shared handles retain existing storage, while owning collections such as Vec, Map, and Set are move-only. Their explicit clone operation duplicates the contents.

Cloning a vector.lyc ↓
(pub fn example () -> i32
  (let ((original (vec 3 5 8))
        (duplicate (clone original)))
    (set (at duplicate 0) 99)
    (at original 0)))
result(example) → 3

Changing the cloned vector leaves the original's first element at 3.

Borrowing

&T permits reads through a reference, while !&T also permits writes. Both borrow an existing value, so its owner must keep it alive for as long as the reference is in use.

Borrowing an owned counter.lyc ↓
(pub type Counter
  ((value i32 0)))

(pub fn increment ((counter !&Counter)) -> void
  (inc [counter value]))

(pub fn read ((counter &Counter)) -> i32
  [counter value])

(pub fn example () -> i32
  (let ((counter (Own (Counter))))
    (increment counter)
    (read counter)))
result(example) → 1

The counter stays alive in its Own box while increment borrows the payload for writing and read borrows for reading. The box is cleaned up when the owning local leaves scope.

Storage types

TypeRelationship
TAn ordinary value
&TA non-owning read reference
!&TA non-owning writable reference
(Own T)Unique boxed ownership
(Shared T)Shared ownership for same-thread use
(ThreadShared T)Shared ownership for cross-thread use
(Weak (Shared T))An observer that does not keep a shared value alive

Aliasing

Several borrows can refer to the same stable storage, including writable borrows. A read reference prevents writes through that particular reference, but another writable reference may still change the value.

Two references to one counter.lyc ↓
(import (examples/ownership (Counter increment read)))

(pub fn increment-both ((left !&Counter) (right !&Counter)) -> i32
  (increment left)
  (increment right)
  (read left))

(pub fn example () -> i32
  (let ((counter (Counter)))
    (increment-both counter counter)))
result(example) → 2

Both parameters borrow the same counter, so each increment updates its value.

The compiler checks borrow provenance, escape, and storage invalidation. A live borrow into a vector's elements constrains operations that could remove or relocate the storage it needs.

Moves and cleanup

Passing a move-only value transfers ownership, while move takes a value explicitly from a field or another writable place. The compiler rejects later uses of consumed storage.

Moving an owned field.lyc ↓
(import (examples/ownership (Counter read)))

(pub type Holder
  ((counter (Own Counter))))

(pub fn take ((holder !&Holder)) -> (Own Counter)
  (move [holder counter]))

(pub fn example () -> i32
  (let ((holder (Holder :counter (Own (Counter :value 5))))
        (counter (take holder)))
    (read counter)))
result(example) → 5

take transfers the box out of the holder. The local counter then owns it until the function returns.

Owned locals are cleaned up when they leave scope, including on an early return, and a drop overload can release the resources held by a product. Shared storage stays alive until its final owner releases it; use a weak handle for a link that should not keep the value alive.

Next pageMacros and compile time