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

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

(with_expansions [<representation> [Type Any]]
  (primitive.def .public Dynamic
    <representation>
    
    (def .public dynamic
      (syntax (_ [value <code>.any])
        (with_symbols [g!value]
          (in (list (` (.let [(, g!value) (, value)]
                         (as Dynamic [(.type_of (, g!value)) (, g!value)]))))))))

    (def .public static
      (syntax (_ [type <code>.any
                  value <code>.any])
        (with_symbols [g!type g!value]
          (in (list (` (.let [[(, g!type) (, g!value)] (|> (, value)
                                                           (is Dynamic)
                                                           (as <representation>))]
                         (.is (try.Try (, type))
                              (.if (.at //.equivalence (,' =)
                                        (.type_literal (, type)) (, g!type))
                                {try.#Success (.as (, type) (, g!value))}
                                (exception.except ..wrong_type [(.type_literal (, type)) (, g!type)]))))))))))

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