Types in shadow mode
Records and enums: declared, built and read. The type checker reports their misuse as findings and does not change what a program does.
The type checker of edition 1 runs in shadow mode: it reports what it finds, and the run of the program is the same with or without it. These examples declare a record and an enum.
Records and enums#
A record type and an enum declared, built and read.
Cuprel
// Types in shadow mode: a record and an enum are declared, constructed and read, and the checker reports their misuse as findings while the run is unchanged.
indicator "Records and enums"
edition = 1
display = pane
enum Trend
up
down
flat
type Reading
average: float = 0
trend: Trend = Trend.flat
input length: int = 20, name = "Length", min = 2
let average = tech.sma(close, length)
let reading = Reading(average = average, trend = if close > average then Trend.up else Trend.down)
plot(reading.average, name = "Average")
plot(if reading.trend == Trend.up then 1 else -1, name = "Direction", style = histogram)Zone with a record#
A record kept in a state, replaced by a new value, and a method defined on the type.
Cuprel
// A record kept in a state, with a method of its own. Zone holds a price band and the number of bars that closed inside it.
// A new band is made every `length` bars from the highest high and lowest low; `zone.touch(close)` is a call of the method defined on the type.
indicator "Zone with a record"
edition = 1
display = price
type Zone
top: float = na
bottom: float = na
inside: int = 0
fn Zone.touch(self: Zone, price: float)
if price <= self.top and price >= self.bottom
self.inside += 1
input length: int = 30, name = "Length", min = 5
let top = tech.highest(high, length)
let bottom = tech.lowest(low, length)
state zone: Zone = Zone()
if bar.index % length == 0
zone = Zone(top = top, bottom = bottom)
zone.touch(close)
plot(zone.top, name = "Top", color = color.red)
plot(zone.bottom, name = "Bottom", color = color.green)
plot(zone.inside, name = "Closes inside", display = none)