I feel like I'm going round in circles here, I'm back to the old @withonly
since using a composite type to allow mutable behaviour leads to this sort
of thing:
type FilterFE1
lp::Float64
g::Float64
end
function lowpass(state::FilterFE1, input::Float64)
# pull state into local variables
g = state.g
lp = state.lp
# now the actual code (imagine this was around 100x more complicated so
you really don't want
# to have state.lp and state.g everywhere since it would make the code
unreadable
hp = input - lp
lp = lp + g * hp
# push local variables into state
state.lp = lp
# return the low pass and high pass outputs
[lp, hp]
end
So that is the result of a rather trivial of lowpass filter examples so
it's gone from a very easy to understand:
function lowpass(input::Float64, mutable lp::Float64, g::Float64)
hp = input - lp
lp += g * hp
[lp, hp]
end
into a much more complicated and error prone beast, which will just get
more complicated the more state there is. (ps: there is no mutable keyword,
I just added that to make it clear the intention of this code).
If there was such a macro as @withonly then it would look more like:
function lowpass(state::FilterFE1, input::Float64)
@withonly state::FilterFE1, input begin
local hp = input - lp
lp = lp + g * hp
[lp, hp]
end
end
which in is a little a lot less brittle and almost as easy to read as the
original code, probably better again would be some kind of "withonly"
function syntax like:
thisfunction lowpass (this::FilterFE1, input::Float64)
local hp = input - lp
lp = lp + g * hp
[lp, hp]
end
And inside the "thisfunction" the first parameter is like a class in c++
where you can refer to the names of this without needing to putting "this."
in front of them, all other arguments passed must not be a name of the this
type, and all local variables must be declared explicitly with the "local"
keyword.