I would have to explicitly state all the variables

Yes, you would.

the Dict that I call the function with is longer “preserved” (correct?) as 
a variable

Assuming this was meant to read “is no longer”, it would not be possible to 
pass it to additional calls.

You might be able to use a composite type (ie. type) if the number and name 
of fields don’t change arbitrarily. Something like:

type State
    x::Int
    y::Int
    z::Float64
    # ...
end

and then pass the entire state object as one of the arguments to your 
methods.

It should also be possible to “unpack” the fields of the state into local 
variables using something like:

macro vars(t, T)
    out = Expr(:block)
    for field in names(getfield(current_module(), T))
        push!(out.args, :($(field) = $(t).$(field)))
    end
    esc(out)
end

and then unpack the fields in the required methods:

function f(s::State)
    @vars s State
    x + y + z
end

(Disclaimer: I’ve not tried to use that @vars macro yet.)

— Mike
​

On Monday, 16 February 2015 15:13:25 UTC+2, Martin Johansson wrote:
>
> Thanks, that's a good point regarding the scope and not a "side effect" I 
> would want.
>
> If I understand the keyword part correctly, I would have to explicitly 
> state all the variables in the function definition to be able to use them 
> locally which, as you write, can become verbose (very verbose for many of 
> my functions, actually). In addition, the Dict that I call the function 
> with is longer "preserved" (correct?) as a variable. I want to supply that 
> Dict to many other functions at various levels below the function I first 
> call. So that will not work. I guess explicitly using the Dict is the best 
> way forward. That's the way I do with structs in Matlab, but with a 
> somewhat more compact notation, so I can live with that.
>
> Regards, m
>

Reply via email to