sigildocs

(sigil struct)

(sigil struct) - Struct Types

Provides define-struct for creating named-field data structures with:

  • Keyword-based construction
  • Optional field defaults
  • Optional per-field mutability
  • Struct embedding via include:

Basic Usage

(import (sigil struct))

(define-struct person
  (name default: "")
  (age default: 0))

;; Constructor uses type name with keywords:
(define alice (person name: "Alice" age: 30))

;; Accessors use typename-fieldname:
(person-name alice)  ; => "Alice"
(person-age alice)   ; => 30

;; Predicate:
(person? alice)      ; => #t

Mutable Fields

Fields are immutable by default. Use mutable: #t for mutability:

(define-struct counter
  (value default: 0 mutable: #t))

(define c (counter))
(set-counter-value! c 10)
(counter-value c)  ; => 10

Copy-Construction

Pass an existing struct as the first argument to copy with modifications:

(define alice (person name: "Alice" age: 30))
(define bob (person alice name: "Bob"))
;; bob has name: "Bob", age: 30

Struct Embedding

Use include: to embed another struct's fields:

(define-struct employee
  include: person
  (department default: "Engineering")
  (salary))

(define emp (employee name: "Alice" age: 30 department: "Eng" salary: 80000))
(employee-name emp)       ; => "Alice" (inherited field)
(employee-department emp) ; => "Eng" (own field)
(employee->person emp)    ; => #<person name: "Alice" age: 30>

Exports

Register a struct type's metadata

Collect all fields from a struct-type object by walking parent chain

Try to look up a struct type from imported modules Looks up the constructor and extracts the type from its metadata. Returns (all-field-names #f) or #f if not found Note: Returns #f for parent since we've already collected all fields

Look up a struct type's metadata First checks local registry, then falls back to imported types Returns (field-names parent-type) or #f if not found

Get all field names from a type (own + inherited). Walks the parent chain via procedure metadata.

Helper: Unwrap syntax object to get datum

Helper: Get docstring from syntax object

Helper: Get srcloc from syntax object

Helper: Process field spec into (name default mutable? docstring)

make-accessorprocedure

Helper: Build accessor definition with docstring

make-mutatorprocedure

Helper: Build mutator definition with auto-generated docstring

The define-struct macro transformer

Define a struct type with named fields.

Creates a constructor, predicate, and field accessors for a new struct type. Fields can have defaults (default:), be mutable (mutable: #t), and structs can embed other structs (include:).

(define-struct person
  (name default: "")
  (age default: 0 mutable: #t))

(define p (person name: "Alice" age: 30))
(person-name p)       ; => "Alice"
(set-person-age! p 31)
struct?variable

Re-exported from (sigil core)

struct-type?variable

Re-exported from (sigil core)

Re-exported from (sigil core)

Re-exported from (sigil core)

Re-exported from (sigil core)

Re-exported from (sigil core)

Re-exported from (sigil core)

Re-exported from (sigil core)