Methods

## Methods

`method name(Type self, ...) => body` at the top level declares a method: a function whose first parameter has a
type. Call it as `x.name(...)` or `name(x, ...)`. The type can be a user type or a builtin one (`float`, `int`,
`string`, `array<float>`, `line`, ...). Methods with the same name for different types are allowed; the one for
the value's type is picked when compiling (an int also finds a float method). A user method comes before a builtin
one of the same name (`a.push(x)` on an `array<float>` uses yours if you declared `method push(array<float> a, float
v)`). Methods are inlined like functions: each call site has its own ta.* state and vars; no recursion.
- Parameters of functions and methods may have a type and a constant default: `f(float x, int n = 3) =>`. A typed
  parameter checks its argument; parameters with defaults come last and may be left out or given by name.
- A one-line body can be any statement: `method set(Level self, float v) => self.price := v`.

```lynt
indicator("Levels with methods", overlay=true)
type Level
    float price
    int touches = 0
method touch(Level self, float tolerance = 0) =>
    if low <= self.price + tolerance and high >= self.price - tolerance
        self.touches += 1
    self
method nearest(array<Level> levels, float p) =>
    Level best = na
    for l in levels
        if na(best) or math.abs(l.price - p) < math.abs(best.price - p)
            best := l
    best
var levels = array.new<Level>()
if session.isfirstbar
    levels.push(Level.new(open))
    if levels.size() > 5
        levels.shift()
for l in levels
    l.touch(syminfo.mintick * 2)
n = levels.nearest(close)
plot(na(n) ? na : n.price, "Nearest level", color=color.amber, style=plot.style_stepline)
```