aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/type/dynamic.lux
blob: 80bc06a84718d7a00e8a7de1f905abb45ec6fafd (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 (.except static)
   ["[0]" debug]
   [control
    ["[0]" try (.only Try)]
    ["[0]" exception (.only exception:)]
    [parser
     ["<[0]>" code]]]
   [data
    [text
     ["%" format]]]
   [macro (.only with_symbols)
    ["[0]" syntax (.only syntax:)]]
   ["[0]" type (.only)
    ["[0]" primitive (.only primitive:)]]]])

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

(primitive: .public Dynamic
  [Type Any]

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

  (syntax: .public (dynamic [value <code>.any])
    (with_symbols [g!value]
      (in (list (` (.let [(~ g!value) (~ value)]
                     ((~! ..abstraction) [(.type_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))]
                     (.is ((~! try.Try) (~ type))
                          (.if (.at (~! 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] (primitive.representation value)]
      (debug.representation type value)))
  )