Skip to content

Commit 0f0dd10

Browse files
authored
Fix value metadata assertions (//=>) (#54)
1 parent a04d372 commit 0f0dd10

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

lib/prelude.eu

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ str: {
352352
export: :suppress }
353353
identity(v): v
354354

355-
` { doc: "`constantly(k)` - return single arg function that always returns k."
355+
` { doc: "`const(k)` - return single arg function that always returns k."
356356
export: :suppress }
357357
const(k, _): k
358358

@@ -415,17 +415,30 @@ with-meta: __WITHMETA
415415
export: :suppress }
416416
meta: __META
417417

418+
` :suppress
419+
assertions: {
420+
# In debug mode maybe the machine checks every return value against
421+
# metadata, but for performance reasons we only check for explicit
422+
# assertions.
423+
424+
` { doc: "`validator(v)` - find the validator for a value `v` in its metadata" }
425+
validator(v): lookup-or(:assert, const(true), meta(v))
426+
427+
` { doc: "`check(v)` - panic if value doesn't satisfy its validator" }
428+
check(v): if(v validator(v), v, panic("assertion failed"))
429+
}
430+
418431
` { doc: "`e //=> v` - add metadata to assert expression `e` evaluates to `v` and return value of `e`."
419432
export: :suppress
420433
associates: :left
421434
precedence: :meta }
422-
(e //=> v): e with-meta({ assert: __EQ(v)})
435+
(e //=> v): e with-meta({ assert: __EQ(v)}) assertions.check
423436

424437
` { doc: "`e //=? f` - add metadata to assert expression `e` satisfies function `f` and return value of `e`."
425438
export: :suppress
426439
associates: :left
427440
precedence: :meta }
428-
(e //=? f): e with-meta({ assert: f})
441+
(e //=? f): e with-meta({ assert: f}) assertions.check
429442

430443

431444
#

0 commit comments

Comments
 (0)