aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/type/dynamic.lux
blob: db594d293c5d920290b0827daedc1ed8b786b549 (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
(.module:
  [library
   [lux #*
    ["." debug]
    [control
     ["." try (#+ Try)]
     ["." exception (#+ exception:)]]
    [data
     [text
      ["%" format]]]
    [macro (#+ with_identifiers)
     ["." syntax (#+ syntax:)]]
    ["." type
     abstract]]])

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

(abstract: .public Dynamic
  {#.doc "A value coupled with its type, so it can be checked later."}

  [Type Any]

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

  (syntax: .public (:dynamic value)
    {#.doc (example (: Dynamic
                       (:dynamic 123)))}
    (with_identifiers [g!value]
      (in (list (` (let [(~ g!value) (~ value)]
                     ((~! ..abstraction) [(:of (~ g!value)) (~ g!value)])))))))

  (syntax: .public (:static type value)
    {#.doc (example (: (try.Try Nat)
                       (:static Nat (:dynamic 123))))}
    (with_identifiers [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)))
  )