kaxori
03/30/2024, 1:56 PMkaxori
03/30/2024, 1:57 PMCODE_INVOCATION_FAILED
Called block with too few arguments.
Got: 0 Expected: 1.
Target:
testList.<block>.<block> test.toit:32:24kaxori
03/30/2024, 2:01 PMkasperl
03/30/2024, 2:33 PMcatch:, you're passing a block to the called method. That block is called without any arguments, so if the block you provide expects one or more arguments, you'll get an exception.kasperl
03/30/2024, 2:33 PMkasperl
03/30/2024, 2:33 PMcatch: | x y | ....kasperl
03/30/2024, 2:34 PMkaxori
03/30/2024, 2:34 PMtestList:
DATA ::= [1,2]
2.repeat: // works
d := DATA[it]
print "$it: $d"
2.repeat:
d := null
e := catch --trace : d = DATA[it]
print "$it: $d"kasperl
03/30/2024, 2:34 PMit.kasperl
03/30/2024, 2:35 PMkasperl
03/30/2024, 2:36 PM2.repeat: | index | catch --trace: DATA[index]kaxori
03/30/2024, 2:43 PMkaxori
03/30/2024, 2:45 PMkaxori
03/30/2024, 2:54 PMkaxori
03/30/2024, 2:55 PMmain:
testList
testList:
DATA ::= [1,2]
// force out of bounds error - but catch it
3.repeat: | index |
d := null
e := catch : d = DATA[index]
print "$index: $d"
Now it works - as expected !
Nevertheless, what is the difference in using 'it' and 'index' ?kasperl
03/30/2024, 3:23 PMit, adds an implicitly defined parameter to the innermost block. In your case that was the block passed to catch:.kasperl
03/30/2024, 3:24 PMit from within the block passed to catch:, so it doesn't get the unwanted parameter.kasperl
03/30/2024, 3:26 PMindex or any other name you explicitly give your block parameters, you do not get any implicitly defined parameters for the block passed to catch:. Instead, the compiler knows that index refers to the parameter in the block passed to repeat:.kaxori
03/30/2024, 3:30 PMkasperl
03/30/2024, 3:35 PMit for "small" blocks. That's where it really shines.