M2 icon indicating copy to clipboard operation
M2 copied to clipboard

try..then without else clause

Open mahrud opened this issue 3 years ago • 4 comments

Currently this doesn't work:

i1 : try true then 2
stdio:1:1:(3): error: syntax error : expected 'else' to match this 'try'

I would have expected it to be treated like this:

i2 : try true then 2 else null

o2 = 2

i3 : try error() then 2 else null

i4 : 

Similar to how if..then..else works:

i1 : if true then 2

o1 = 2

i2 : if true then 2 else null

o2 = 2

i3 : if false then 2 else null

i4 : 

mahrud avatar Aug 19 '22 08:08 mahrud

Such syntax is not needed, for one can simply write try ( foo ; 2 ) else null. Then if no error occurs in foo, the value will be 2.

DanGrayson avatar Aug 19 '22 13:08 DanGrayson

The syntax is cleaner without parentheses, so what's the harm?

mahrud avatar Aug 19 '22 13:08 mahrud

Well, if the then clause is not mandatory, there's no harm, but where's the gain that corresponds to all that work? Usually, one wants the value of foo, unless some error occurs, so one writes something like try foo else 3331333.

DanGrayson avatar Aug 19 '22 14:08 DanGrayson