onlycall

onlycall, function

def pure onlycall(max: number): boolean

Reports an error and causes the script to fail if called more than max times in a single execution scope. Otherwise, returns true.

Example

table T = extend.range(4)
T.Guard = if onlycall(10) then T.N else 0

show table "Guarded values" with
  T.N
  T.Guard

This outputs the following table:

N Guard
1 1
2 2
3 3
4 4

Remarks

Use onlycall in an if expression so the call is not optimized away:

table T = extend.range(1)
T.Value = if onlycall(1) then 42 else 0

Two separate uses of onlycall in different locations count their executions separately. Each iteration of a loop block counts as a separate location.

On small tables and in non-parallel for, each, montecarlo, or autodiff blocks, a single call site counts every execution. Outside of those cases, execution may be partitioned and onlycall can underestimate the actual number of calls.

Errors

Calling onlycall more than its specified number of times (or specifying a number of times < 1) reports an error message:

‘onlycall()’: called 5 times.

User Contributed Notes
0 notes + add a note