blob: d50fefc27e5bfd41e21a4c95083c3fec1f58e1ee (
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
44
45
46
47
48
49
50
|
(.module:
[lux #*
["." debug]
[control
["." try (#+ Try)]
["." exception (#+ exception:)]]
[data
[text
["%" format]]]
[macro (#+ with_gensyms)
["." syntax (#+ syntax:)]]
["." type
abstract]])
(exception: #export (wrong_type {expected Type} {actual Type})
(exception.report
["Expected" (%.type expected)]
["Actual" (%.type actual)]))
(abstract: #export Dynamic
[Type Any]
{#.doc "A value coupled with its type, so it can be checked later."}
(def: abstraction (-> [Type Any] Dynamic) (|>> :abstraction))
(def: representation (-> Dynamic [Type Any]) (|>> :representation))
(syntax: #export (:dynamic value)
{#.doc (doc (: Dynamic
(:dynamic 123)))}
(with_gensyms [g!value]
(wrap (list (` (let [(~ g!value) (~ value)]
((~! ..abstraction) [(:of (~ g!value)) (~ g!value)])))))))
(syntax: #export (:check type value)
{#.doc (doc (: (try.Try Nat)
(:check Nat (:dynamic 123))))}
(with_gensyms [g!type g!value]
(wrap (list (` (let [[(~ g!type) (~ g!value)] ((~! ..representation) (~ value))]
(: ((~! try.Try) (~ type))
(if (\ (~! type.equivalence) (~' =)
(.type (~ type)) (~ g!type))
(#try.Success (:coerce (~ type) (~ g!value)))
((~! exception.throw) ..wrong_type [(.type (~ type)) (~ g!type)])))))))))
(def: #export (format value)
(-> Dynamic (Try Text))
(let [[type value] (:representation value)]
(debug.represent type value)))
)
|