-
Notifications
You must be signed in to change notification settings - Fork 3
/
typeof-1.0.0.tm
66 lines (66 loc) · 1.82 KB
/
typeof-1.0.0.tm
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
63
64
65
66
if 0 {
@ typeof @
| Attempts to capture the "type" of the value by
| checking the underlying tcl representation of the
| value.
> Important <
| This can not be depended upon as values representations
| may change at various points when code is evaluated.
@value {any}
The value that we want the type for.
@args {?list<-opts>?}
Optional opts to modify the execution of the
function.
@arg -exact
Does not parse the value returned into simpler representations.
For example, it wont check if "pure string" is actually a "number".
@arg -deep
Attempts to resolve the type in a deep manner. This will follow
dicts and lists, checking the type of their values.
}
proc typeof {value args} {
regexp {^value is a (.*?) with a refcount} \
[::tcl::unsupported::representation $value] -> type
# parse the type to return simpler representations
if {"-exact" ni $args} {
switch -glob -- $type {
boolean* {
set type boolean
}
*string {
# parse string to see if it is actually another value
if {[string is entier -strict $value]} {
set type number
} elseif {[string is double -strict $value]} {
set type number
} elseif {[string is boolean -strict $value]} {
set type boolean
} else {
set type string
}
}
int - double {
set type number
}
}
}
if {"-deep" in $args} {
switch -- $type {
list {
set reps [list]
foreach el $value {
lappend reps [typeof $el {*}$args]
}
lappend type $reps
}
dict {
set reps [dict create]
dict for {k v} $value {
dict set reps $k [typeof $v {*}$args]
}
lappend type $reps
}
}
}
return $type
}