Dear all,
I have a question about the use of custom constructor to initialize a Julia struct. I would like to initialize a parameter z (operational parameter) of a structure from some physical parameters, e.g. x and y. To clarify the problem, let’s consider the following code snippet
# ERROR
struct Foo
z
function Foo(x, y)
self = new()
self.z = x + y
return self
end
end
This code raises an error. To make it work, I need to declare the structure Foo as mutable, which is not desired proper, since z doesn’t mutate.
mutable struct Foo
z
function Foo(x, y)
self = new()
self.z = x + y
return self
end
end
What is the Julian way to do this (i.e. keep the structure immutable, while initializing it from external parameters) ?
Thank you for your answers.