-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_support.clj
62 lines (42 loc) · 1.8 KB
/
type_support.clj
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
55
56
57
58
59
60
61
62
(ns properties.type-support)
(defmulti implicit-default
"This multimethod returns the implicit default value for a given type. If the
type has no method, the multimethod returns nil.
Parameters:
prop-type - the type to find the implicit default for
Returns:
It returns the implicit default value for the given type. If there isn't
one, it returns nil."
(fn [prop-type] prop-type))
(defmethod implicit-default :default [_] nil)
(defmulti ^Boolean type?
"This multimethod acts as a predicate to detect if a given value has a given
type. If the type has no method, the result will check the type of the value
to see if it is a subtype of the dispatch type.
Parameters:
prop-type - the type the value should have
value - the value being tested.
Returns:
It returns true, if the value is of the correct type, otherwise false."
(fn [prop-type value] prop-type))
(defmethod type? :default [prop-type value] (isa? (type value) prop-type))
(defmulti from-str
"Given a type, this multimethod converts a string to a value of that type. If
the type has no method, the read-string function will be used.
Parameters:
prop-type - the type used to dispatch on
str-val - the string to parse
Returns:
It returns the value from the string."
(fn [prop-type ^String str-val] prop-type))
(defmethod from-str :default [_ str-val] (read-string str-val))
(defmulti as-code
"Given a value, this multimethod will prepare this value for runtime code
generation. The multimethod dispatches on the type of the value. If the type
has no method, the value will be returned unaltered.
Parameters:
value - the value to encode
Returns:
The encoded value is returned."
(fn [value] (type value)))
(defmethod as-code :default [value] value)