aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/type/dynamic.lux
blob: 28c9b7e35c6dfc2647f56e19c0930332851d0b2c (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
51
52
53
54
(.using
 [library
  [lux "*"
   ["[0]" debug]
   [control
    ["[0]" try {"+" Try}]
    ["[0]" exception {"+" exception:}]
    [parser
     ["<[0]>" code]]]
   [data
    [text
     ["%" format]]]
   [macro {"+" with_symbols}
    ["[0]" syntax {"+" syntax:}]]
   ["[0]" type
    abstract]]])

(exception: .public (wrong_type [expected Type
                                 actual Type])
  (exception.report
   "Expected" (%.type expected)
   "Actual" (%.type actual)))

(abstract: .public Dynamic
  [Type Any]

  (def: abstraction
    (-> [Type Any] Dynamic)
    (|>> :abstraction))
  
  (def: representation
    (-> Dynamic [Type Any])
    (|>> :representation))

  (syntax: .public (:dynamic [value <code>.any])
    (with_symbols [g!value]
      (in (list (` (let [(~ g!value) (~ value)]
                     ((~! ..abstraction) [(:of (~ g!value)) (~ g!value)])))))))

  (syntax: .public (:static [type <code>.any
                             value <code>.any])
    (with_symbols [g!type g!value]
      (in (list (` (let [[(~ g!type) (~ g!value)] ((~! ..representation) (~ value))]
                     (: ((~! try.Try) (~ type))
                        (if (# (~! type.equivalence) (~' =)
                               (.type (~ type)) (~ g!type))
                          {try.#Success (:as (~ type) (~ g!value))}
                          ((~! exception.except) ..wrong_type [(.type (~ type)) (~ g!type)])))))))))

  (def: .public (format value)
    (-> Dynamic (Try Text))
    (let [[type value] (:representation value)]
      (debug.representation type value)))
  )