I have translated SAMIN (optimize) in the Optim package to another language (where it is needed) and need to add print statements to the Julia code for comparison. This is the code I wish to run:
I have placed samin.jl in my own directory (separate from the julia distribution). When I type: include(“samin.jl”) in the REPL I get: ERROR: LoadError: UndefVarError: @with_kw not defined
Most .jl files in packages are not supposed to be evaluated in isolation, but within another primary file. In Optim.jl/src/Optim.jl, there is a line include("multivariate/solvers/constrained/samin.jl") occurring far after a line import Parameters: @with_kw, @unpack, among other imports and includes. To make your println version work, you would need to replicate the minimum imports and includes that the original needed to work.
I don’t think it’s worth manually doing that because at worst case, you need every preceding line and you might as well fork the whole package at that point. Looking at local variables while debugging is a typical alternative, but it can be too hands-on and slow compared to a run-once-print-all approach. You could load the Optim package and @eval Optim ... modified functions right into the active Optim module, and it will work like the original in most cases. Make sure to change the function name or dispatch on a custom-typed argument so you’re not replacing the original code.
Another idea is maybe to instead of ]add Optim.jl to your environment you ]dev it instead. That creates a copy of the fully module in your .julia/dev (not quite sure about this path, haven’t double checked). There you can modify the code, i.e. add the printlns. Then you can just run a test code normally.