FlowscopeDocs

N002

null check on a field does not narrow it

Severity Error

A field or element (such as chart.close, s.price or xs[0]) was checked against null, and then used as if it were non-null. Narrowing only applies to local bindings: a field can be read again from a different place, so the check does not carry over to the next read.

Copy the value into a let local, check the local, and use the local afterwards.

Failing example

Flowscope Script
script "OI change"
data oi = subscribe(data.oi)
fn pct(now: float, before: float) -> float {
  return (now - before) / before * 100.0
}
pane p = pane(title: "OI change %", height: 0.2)
plot ch = plot.columns(title: "OI change", on: p)
on oi.close {
  if oi.close != null && oi.close[1] != null {
    ch.plot(pct(oi.close, oi.close[1]))
  }
}

Fixed example

Flowscope Script
script "OI change"
data oi = subscribe(data.oi)
fn pct(now: float, before: float) -> float {
  return (now - before) / before * 100.0
}
pane p = pane(title: "OI change %", height: 0.2)
plot ch = plot.columns(title: "OI change", on: p)
on oi.close {
  let now = oi.close
  let before = oi.close[1]
  if now != null && before != null {
    ch.plot(pct(now, before))
  }
}