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