Make it possible to return a value from if, case, etc.
Clarification and motivation
In Indigo we have case_
and other specialized case eliminators (if_
, ifJust
) that always return ()
(and take functions returning ()
). That's in line with imperative languages such as C++, you can't return anything from if
and similar things there. But even though we have imperative things in Indigo, it is still based on Haskell and I think ideally it should have useful features of Haskell.
For example, if we want to write a function whose return value (stored in Var
) depends on some condition, we have to create a variable with some default value, set it inside if_
bodies and return it.
int i;
if a {
i = 2;
} else {
i = 3;
}
Here int i;
puts some default value into i
.
Since this is how it works in C++, I think existing approach is acceptable. But I think it would be much better if we could just write i = if a 2 3
(or setVar i =<< if_ a 2 3
).
At the very least we can implement it using setVar
. So we create a variable with Nothing
inside and call case eliminator where we call setVar
in each case. And then we assert that is not Nothing
. Though the latter is problematic because for that we need case eliminator
Acceptance criteria
All "case-eliminator" instructions should allow to return a value from them other than ()
.