blob: 6fb83f8fbf458b5273d5ad4e87f936841b217184 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
(.module:
[lux #*
[control
["ex" exception (#+ exception:)]]
[data
["." error]
[text
format]]
[macro (#+ with-gensyms)
["." syntax (#+ syntax:)]]
["." type
abstract]])
(exception: #export (wrong-type {expected Type} {actual Type})
(ex.report ["Expected" (%type expected)]
["Actual" (%type actual)]))
(abstract: #export Dynamic
{#.doc "A value coupled with its type, so it can be checked later."}
[Type Any]
(def: dynamic-abstraction (-> [Type Any] Dynamic) (|>> :abstraction))
(def: dynamic-representation (-> Dynamic [Type Any]) (|>> :representation))
(syntax: #export (:dynamic value)
{#.doc (doc (: Dynamic
(:dynamic 123)))}
(with-gensyms [g!value]
(wrap (list (` (let [(~ g!value) (~ value)]
((~! ..dynamic-abstraction) [(:of (~ g!value)) (~ g!value)])))))))
(syntax: #export (:check type value)
{#.doc (doc (: (error.Error Nat)
(:check Nat (:dynamic 123))))}
(with-gensyms [g!type g!value]
(wrap (list (` (let [[(~ g!type) (~ g!value)] ((~! ..dynamic-representation) (~ value))]
(: ((~! error.Error) (~ type))
(if (:: (~! type.Equivalence<Type>) (~' =)
(.type (~ type)) (~ g!type))
(#error.Success (:coerce (~ type) (~ g!value)))
((~! ex.throw) ..wrong-type [(.type (~ type)) (~ g!type)])))))))))
)
|