I am trying to avoid repeatedly passing a struct in my functions in order to access its fields. In Java this can be avoided by using a class and defining instance variables. What's the equivalent in Julia?
Here is some sample code where I am currently writing functions in the style of f, which requires me to pass the struct Param, but ideally I would like to write them in the style of g so that I don't need to pass the struct. It seems like a redundant way of writing things, especially if I have multiple functions that need access to the fields and values in Param.
struct Param
foo::Int
bar::Bool
end
f(n::Int, param::Param) = n + param.foo
g(n::Int) = n + foo
Is it accepted practice to perhaps include the function within the body of the struct?
Your function
fis considered idiomatic in Julia and in most functional programing languages. However, there are a couple patterns to be aware of that you might find useful in some situations.First, there is syntax sugar for unpacking the properties of a struct in the argument of a function, like this:
You can also use a similar syntax inside a function to unpack the properties of a struct:
Second, in some situations it might make sense to use closures to capture the value of a struct. Here is a toy example of how you can do that:
Inside
make_foo_bar, I've named the functionsfunc1andfunc2to emphasize that the names of the functions insidemake_foo_barcan be different from the names of the variables to which you assign the output functions. In this example,fooandbarare variables to which two different anonymous functions are assigned.