Why would a language NOT use Short-circuit evaluation?

后端 未结 18 1983
天涯浪人
天涯浪人 2020-12-03 09:42

Why would a language NOT use Short-circuit evaluation? Are there any benefits of not using it?

I see that it could lead to some performances issues... is that true?

18条回答
  •  执念已碎
    2020-12-03 10:16

    The language Lustre does not use short-circuit evaluation. In if-then-elses, both then and else branches are evaluated at each tick, and one is considered the result of the conditional depending on the evaluation of the condition.

    The reason is that this language, and other synchronous dataflow languages, have a concise syntax to speak of the past. Each branch needs to be computed so that the past of each is available if it becomes necessary in future cycles. The language is supposed to be functional, so that wouldn't matter, but you may call C functions from it (and perhaps notice they are called more often than you thought).

    In Lustre, writing the equivalent of

    if (y <> 0) then 100/y else 100

    is a typical beginner mistake. The division by zero is not avoided, because the expression 100/y is evaluated even on cycles when y=0.

提交回复
热议问题