Expression Short Circuiting

   1372   1   0
User Avatar
Member
6 posts
Joined: 2月 2016
Offline
Today I noticed that expressions do not short circuit. Is this expected with vex?

I get an error when running the following:
function int fail()
{
    error('No short circuiting');
    return 2;
}


i@value = 1 || fail();

In other similar languages, fail() would never have been called.

I suppose the takeaway for me is to be aware of side effects that a non-short circuited function call might have (like a heavy computation).

Also, I don't suppose there's anyway around this except some ridiculous looking code:
int bool = 0;
if(1)
{
    i@value = 1;
    bool = 1;
}

if(!bool)
{
    i@value = fail();
}
Edited by bradley.henke - 2022年12月23日 15:37:28
User Avatar
スタッフ
6413 posts
Joined: 7月 2005
Offline
You are correct that VEX doesn't short circuit. I can't say we are happy with that, as it is very useful and expected to short circuit. I'd say we want to reserve the right to fix this some day, so please do not write code that requires us to not short circuit in the meantime.
  • Quick Links