I would like the initialization of b to refer to the initial value of a, but the following does not work:
x = Example(a = 6, b = a+9)
because the “a” that appears in the definition of b does not refer to the field named a but to some external (undefined) a. Is there a clean way for the initialization of one field to refer to the value stored in a previously defined field? In C++, the keyword “this” addresses this situation.
Thanks for the prompt answers to my question! To clarify, the actual struct in my code has many fields (more than 20), and the relationships among them do not follow any particular pattern. Hence it would be awkward to solve the problem with custom constructors, especially since the code is under development and the precise relationships between the fields may change over time.
OK, I tried to code your suggestion, but it didn’t work. As you can see from the run below, the code reproduces the same problem I had originally, namely, that “a” that appears in the assignment statement b=a+2 does not refer to the “a” that was just initialized. I’m not an expert on macros, and I’m not sure why the macro-expander decided that “a” in one place is a new temporary variable and in the other place is a previously defined variable. Maybe I need to throw in some escapes?
let var"#36#a" = 7, var"#35#b" = Main.test_kwconstruct.a + 2
#= u:\JuliaFEM\CohesiveFEM\src\kwconstruct_helper.jl:40 =#
Main.test_kwconstruct.Example(; a = var"#36#a", b = var"#35#b")
end