| 1 | # tea functions |
| 2 | |
| 3 | #### setvar f()[2] = 42 (setitem) |
| 4 | shopt -s oil:all |
| 5 | shopt -s parse_tea |
| 6 | |
| 7 | var mylist = [1,2,3] |
| 8 | func f() { |
| 9 | return mylist |
| 10 | } |
| 11 | setvar f()[2] = 42 |
| 12 | write @mylist |
| 13 | ## STDOUT: |
| 14 | 1 |
| 15 | 2 |
| 16 | 42 |
| 17 | ## END |
| 18 | |
| 19 | #### Untyped func |
| 20 | shopt -s oil:all |
| 21 | shopt -s parse_tea |
| 22 | |
| 23 | func add(x, y) Int { |
| 24 | echo 'hi' |
| 25 | return x + y |
| 26 | } |
| 27 | var result = add(42, 1) |
| 28 | echo $result |
| 29 | ## STDOUT: |
| 30 | hi |
| 31 | 43 |
| 32 | ## END |
| 33 | |
| 34 | #### Typed func |
| 35 | shopt -s oil:all |
| 36 | shopt -s parse_tea |
| 37 | |
| 38 | func add(x Int, y Int) Int { |
| 39 | echo 'hi' |
| 40 | return x+y |
| 41 | } |
| 42 | var result = add(42, 1) |
| 43 | echo $result |
| 44 | ## STDOUT: |
| 45 | hi |
| 46 | 43 |
| 47 | ## END |
| 48 | |
| 49 | #### func: default values for positional params |
| 50 | shopt -s oil:all |
| 51 | shopt -s parse_tea |
| 52 | |
| 53 | func add(x Int, y=1, z=1+2*3) { |
| 54 | return x + y + z |
| 55 | } |
| 56 | echo $[add(3)] |
| 57 | echo $[add(3,4)] |
| 58 | ## STDOUT: |
| 59 | 11 |
| 60 | 14 |
| 61 | ## END |
| 62 | |
| 63 | #### pass too many positional params to func (without spread) |
| 64 | shopt -s oil:all |
| 65 | shopt -s parse_tea |
| 66 | |
| 67 | func add(x, y) { |
| 68 | return x + y |
| 69 | } |
| 70 | var f = add(1,2) |
| 71 | echo f=$f |
| 72 | var f = add(1,2,3) |
| 73 | echo $f |
| 74 | ## status: 1 |
| 75 | ## STDOUT: |
| 76 | f=3 |
| 77 | ## END |
| 78 | |
| 79 | #### Positional Spread |
| 80 | shopt -s oil:all |
| 81 | shopt -s parse_tea |
| 82 | |
| 83 | func add(x, y, ...args) { |
| 84 | pp cell ':args' # This works in Tea too for debugging |
| 85 | return x + y |
| 86 | } |
| 87 | var args = %[5 6 7 8] |
| 88 | var y = add(...args) |
| 89 | echo y=$y |
| 90 | ## STDOUT: |
| 91 | 7 |
| 92 | 8 |
| 93 | y=11 |
| 94 | ## END |
| 95 | |
| 96 | #### pass named arg to func |
| 97 | shopt -s oil:all |
| 98 | shopt -s parse_tea |
| 99 | |
| 100 | func f(; x=42) { |
| 101 | echo $x |
| 102 | } |
| 103 | _ f() |
| 104 | _ f(x=99) |
| 105 | ## STDOUT: |
| 106 | 42 |
| 107 | 99 |
| 108 | ## END |
| 109 | |
| 110 | #### Func with missing named param with no default |
| 111 | shopt -s oil:all |
| 112 | shopt -s parse_tea |
| 113 | |
| 114 | func add(x Int, y Int ; verbose Bool) { |
| 115 | if (verbose) { |
| 116 | echo 'verbose' |
| 117 | } |
| 118 | return x + y |
| 119 | } |
| 120 | var a = add(3, 2, verbose=true) |
| 121 | echo $a |
| 122 | |
| 123 | # crashes |
| 124 | var a = add(3, 2) |
| 125 | echo "shouldn't get here" |
| 126 | ## status: 1 |
| 127 | ## STDOUT: |
| 128 | verbose |
| 129 | 5 |
| 130 | ## END |
| 131 | |
| 132 | #### Func passed wrong named param |
| 133 | shopt -s oil:all |
| 134 | shopt -s parse_tea |
| 135 | |
| 136 | func add(x, y) { |
| 137 | return x + y |
| 138 | } |
| 139 | var x = add(2, 3) |
| 140 | echo x=$x |
| 141 | var y = add(2, 3, verbose=true) |
| 142 | |
| 143 | ## status: 1 |
| 144 | ## STDOUT: |
| 145 | x=5 |
| 146 | ## END |
| 147 | |
| 148 | |
| 149 | #### Named Spread |
| 150 | shopt -s oil:all |
| 151 | shopt -s parse_tea |
| 152 | |
| 153 | func add(x, y; verbose=true, ...named) { |
| 154 | if (verbose) { echo 'verbose' } |
| 155 | |
| 156 | # Can list splatting work in tea? Maybe it should be |
| 157 | # x = splice(named) |
| 158 | # pp cell 'x' |
| 159 | # Or just |
| 160 | # = named |
| 161 | # = splice(named) |
| 162 | write @named | sort |
| 163 | return x + y |
| 164 | } |
| 165 | var args = {verbose: false, a: 1, b: 2} |
| 166 | var args2 = {f: 3} |
| 167 | var ret = add(2, 3; ...args, ...args2) |
| 168 | echo ret=$ret |
| 169 | ## STDOUT: |
| 170 | a |
| 171 | b |
| 172 | f |
| 173 | ret=5 |
| 174 | ## END |
| 175 | |
| 176 | #### Func with varargs |
| 177 | shopt -s oil:all |
| 178 | shopt -s parse_tea |
| 179 | |
| 180 | func printf(fmt, ...args) { |
| 181 | = fmt |
| 182 | # Should be a LIST |
| 183 | = args |
| 184 | } |
| 185 | _ printf('foo', 'a', 42, null) |
| 186 | |
| 187 | ## STDOUT: |
| 188 | (Str) 'foo' |
| 189 | (Tuple) ('a', 42, None) |
| 190 | ## END |
| 191 | |
| 192 | #### return expression then return builtin |
| 193 | shopt -s parse_tea |
| 194 | |
| 195 | func f(x) { |
| 196 | return x + 2*3 |
| 197 | } |
| 198 | # this goes in proc |
| 199 | f() { |
| 200 | local x=42 |
| 201 | return $x |
| 202 | } |
| 203 | var x = f(36) |
| 204 | echo x=$x |
| 205 | f |
| 206 | echo status=$? |
| 207 | ## STDOUT: |
| 208 | x=42 |
| 209 | status=42 |
| 210 | ## END |
| 211 | |
| 212 | #### inline function calls with spread, named args, etc. |
| 213 | shopt -s oil:all |
| 214 | |
| 215 | func f(a, b=0, ...args; c, d=0, ...named) { |
| 216 | write __ args: @args |
| 217 | write __ named: |
| 218 | write @named | sort |
| 219 | if (named) { |
| 220 | return [a, b, c, d] |
| 221 | } else { |
| 222 | return a + b + c + d |
| 223 | } |
| 224 | } |
| 225 | var a = [42, 43] |
| 226 | var n = %{x: 99, y: 100} |
| 227 | |
| 228 | echo ____ |
| 229 | write string $[f(0, 1, ...a, c=2, d=3)] |
| 230 | |
| 231 | # Now get a list back |
| 232 | echo ____ |
| 233 | write array @[f(5, 6, ...a, c=7, d=8; ...n)] |
| 234 | |
| 235 | ## STDOUT: |
| 236 | ____ |
| 237 | __ |
| 238 | args: |
| 239 | 42 |
| 240 | 43 |
| 241 | __ |
| 242 | named: |
| 243 | |
| 244 | string |
| 245 | 6 |
| 246 | ____ |
| 247 | __ |
| 248 | args: |
| 249 | 42 |
| 250 | 43 |
| 251 | __ |
| 252 | named: |
| 253 | x |
| 254 | y |
| 255 | array |
| 256 | 5 |
| 257 | 6 |
| 258 | 7 |
| 259 | 8 |
| 260 | ## END |
| 261 | |
| 262 | #### basic lambda |
| 263 | var f = |x| x+1 |
| 264 | var y = f(0) |
| 265 | echo $y |
| 266 | echo $[f(42)] |
| 267 | ## STDOUT: |
| 268 | 1 |
| 269 | 43 |
| 270 | ## END |
| 271 | |
| 272 | #### Nested functions ??? Bug from Till. |
| 273 | shopt -s oil:all |
| 274 | shopt -s parse_tea |
| 275 | |
| 276 | # I think we should disallow these. |
| 277 | # The bug is related to return as a keyword!!! Which we will get rid of in the |
| 278 | # newer Tea parser. |
| 279 | |
| 280 | func returnsString() { |
| 281 | func unrelated() {return "hm?"} |
| 282 | return "test" # This fails |
| 283 | } |
| 284 | |
| 285 | = returnsString() |
| 286 | |
| 287 | ## STDOUT: |
| 288 | ## END |