Hi Alan,
Nice observation! Although it says "runtime error", it actually happens at
compile time, as NetLogo evaluates constant expressions while compiling.
This is done both as an optimization as well as to catch necessarily
incorrect code. To see this, try putting that code in the code tab and
hitting "check". You'll get the error despite nothing actually
running. `ifelse-value` also isn't special here. For instance, the
following have the same behavior:
ifelse true [ show 0 ] [ show 0 / 0 ]
ask turtles [ show 0 / 0 ] ;; even when there are no turtles
let im-not-run task [ 0 / 0 ]
There are a few other operations that show the same behavior. For instance,
`log 0 10` and `sqrt -1` (since NetLogo doesn't have imaginary numbers)
both error in a similar manner.
> Is it certain that this will never happen if the expressions involve
variables?
Yes, it's certain. Only constant expressions undergo this treatment. During
actual runtime, the second block in that `ifelse-value` will never be
evaluated, as demonstrated by the fact that this code runs just fine:
let zero 0 show ifelse-value true [ zero ] [ zero / zero ]
If you're curious, the specific code responsible for this can be found in
ConstantFolder
<https://github.com/NetLogo/NetLogo/blob/380863baf1d0d403dde446e15d88b68033f338ae/src/main/org/nlogo/compiler/ConstantFolder.scala#L29>.
Many primitives (such as `/`, `log`, `sqrt`, etc) are marked as `Pure`,
meaning that they will always give the same output for the same input.
Thus, when given constants, they can be evaluated at compile time so they
don't have to be evaluated at runtime.
Best,
Bryan
On Sat, Feb 20, 2016 at 1:49 PM Alan Isaac <[email protected]> wrote:
> Is it intentional that `ifelse-value true [0] [0 / 0]` raises a runtime
> error?
> Is it certain that this will never happen if the expressions involve
> variables?
>
> Thanks,
> Alan Isaac
>
> --
> You received this message because you are subscribed to the Google Groups
> "netlogo-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"netlogo-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.