(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) ; => #tMutable 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) ; => 10Copy-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: 30Struct 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-struct-type!procedureRegister a struct type's metadata
%collect-fields-from-typeprocedureCollect all fields from a struct-type object by walking parent chain
%lookup-imported-struct-typeprocedureTry 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
%lookup-struct-typeprocedureLook 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-inherited-fieldsprocedureGet all field names from a type (own + inherited). Walks the parent chain via procedure metadata.
struct--unwrapprocedureHelper: Unwrap syntax object to get datum
struct--get-docprocedureHelper: Get docstring from syntax object
struct--get-srclocprocedureHelper: Get srcloc from syntax object
parse-field-specprocedureHelper: Process field spec into (name default mutable? docstring)
make-accessorprocedureHelper: Build accessor definition with docstring
make-mutatorprocedureHelper: Build mutator definition with auto-generated docstring
struct-transformerprocedureThe define-struct macro transformer
define-structsyntaxDefine 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?variableRe-exported from (sigil core)
struct-type?variableRe-exported from (sigil core)
struct-type-ofvariableRe-exported from (sigil core)
struct-type-namevariableRe-exported from (sigil core)
struct-type-field-countvariableRe-exported from (sigil core)
struct-type-field-namesvariableRe-exported from (sigil core)
struct-type-parentvariableRe-exported from (sigil core)
struct-field-indexvariableRe-exported from (sigil core)