Files
RustAst/examples/records.myc
T
Brummel 16af3ae9fc Refactor Examples to use assert_eq
This commit updates all example files to use `assert_eq!` for verifying
output, replacing the previous `Output:` comments. This change makes the
examples more robust and self-testing.

The benchmark results in some examples have also been slightly adjusted,
reflecting minor performance variations after the refactoring.

Additionally, a runtime panic handling mechanism has been introduced in
the VM for function calls, which improves error reporting for unexpected
panics.
2026-03-26 15:07:48 +01:00

53 lines
1.4 KiB
Plaintext

;; Benchmark: 1.3us
;; Benchmark-Repeat: 1597
; ---------------------------------------------------------
; Records & Optimized Field Access Showcase
; ---------------------------------------------------------
(do
; 1. Record Creation
; Records use interned layouts for O(1) access and low memory overhead.
(def user {:id 101 :name "Alice" :role :admin})
; 2. Optimized Field Access
; These calls are transformed by the optimizer into specialized instructions.
(def name (.name user))
(def id (.id user))
; 3. First-Class Field Accessors
; Accessors start with a dot and can be passed around like functions.
(def get-role .role)
(def role (get-role user))
; 4. Nested Records
(def company {
:name "Myc Corp"
:location {:city "Zürich" :zip 8000}
:employees [user {:id 102 :name "Bob" :role :dev}]
})
; Deep access (also optimized)
(def city (.city (.location company)))
; 5. Higher-Order usage
; Accessors are perfect for mapping over data.
(def names ((fn [f list]
(do
(def [e1 e2] list)
[(f e1) (f e2)]
))
.name (.employees company)))
; 6. Structural Identity
; Records with the same layout share memory; equality checks values.
(def is-equal (= {:x 1 :y 2} {:x 1 :y 2}))
(assert-eq "Alice" name)
(assert-eq 101 id)
(assert-eq :admin role)
(assert-eq "Zürich" city)
(assert-eq ["Alice" "Bob"] names)
(assert is-equal)
)