1
2
3 #### bool() conversion
4 echo "$[bool(1234)]"
5 echo "$[bool(0)]"
6 echo "$[bool('foo')]"
7 echo "$[bool(true)]"
8 echo "$[bool(1.234)]"
9 echo "$[bool([])]"
10 echo "$[bool({})]"
11 echo "$[bool(null)]"
12 echo "$[bool(len)]"
13 echo "$[bool('foo'=>startsWith)]"
14 echo "$[bool(1..3)]"
15 ## STDOUT:
16 true
17 false
18 true
19 true
20 true
21 false
22 false
23 false
24 true
25 true
26 true
27 ## END
28
29 #### bool() more
30 var a = bool( :|| )
31 var b = bool( :|foo| )
32 write $a $b
33 ## STDOUT:
34 false
35 true
36 ## END
37
38
39 #### int() conversion
40 echo "$[int(1234)]"
41 echo "$[int('1234')]"
42 echo "$[int(1.234)]"
43 ## STDOUT:
44 1234
45 1234
46 1
47 ## END
48
49 #### int() more
50 var a = int("3")
51 var b = int("-35")
52 write $a $b
53
54 var c = int("bad")
55 echo 'should not get here'
56
57 ## status: 3
58 ## STDOUT:
59 3
60 -35
61 ## END
62
63 #### float() conversion
64 echo "$[float(1234)]"
65 echo "$[float('1.234')]"
66 echo "$[float(2.345)]"
67 ## STDOUT:
68 1234.0
69 1.234
70 2.345
71 ## END
72
73 #### float() overflow / underflow
74
75 var a = float("1.2")
76 var b = float("3.4")
77
78 var c = float("42.1e500")
79 var d = float("-42.1e500")
80
81 write $a $b $c $d
82 ## STDOUT:
83 1.2
84 3.4
85 inf
86 -inf
87 ## END
88
89 #### str() conversion
90 echo "$[str(1234)]"
91 echo "$[str(1.234)]"
92 echo "$[str('foo')]"
93 ## STDOUT:
94 1234
95 1.234
96 foo
97 ## END
98
99 #### dict() converts from BashAssoc to Dict
100 declare -A foo
101 foo=([a]=1 [b]=2 [c]=3)
102
103 json write (type(foo))
104 json write (dict(foo))
105 ## STDOUT:
106 "BashAssoc"
107 {
108 "a": "1",
109 "b": "2",
110 "c": "3"
111 }
112 ## END
113
114 #### dict() does shallow copy
115 var d = {'a': 1}
116 var d2 = d
117 setvar d2['b'] = 2
118 echo $['b' in d] # d2 should be an alias for d
119
120 var d3 = dict(d)
121 setvar d3['c'] = 3
122
123 # d3 should NOT be an alias
124 echo $['c' in d]
125 echo $['c' in d3]
126 ## STDOUT:
127 true
128 false
129 true
130 ## END
131
132 #### list() does shallow copy
133 var l = [1]
134 var l2 = l
135 call l2->append(2)
136 echo $[len(l)] # d2 should be an alias for d
137
138 var l3 = list(l)
139 call l3->append(3)
140
141 # l3 should NOT be an alias
142 echo $[len(l)]
143 echo $[len(l3)]
144 ## STDOUT:
145 2
146 2
147 3
148 ## END
149
150 #### list() from Dict
151 shopt -s ysh:upgrade
152
153 var a = list({'a': 1, 'foo': 'bar'})
154 write @a
155 ## STDOUT:
156 a
157 foo
158 ## END
159
160
161 #### list() from range
162 shopt -s ysh:upgrade
163
164 var mylist = list(0..3)
165 write @mylist
166 ## STDOUT:
167 0
168 1
169 2
170 ## END
171