Workflow with redefining structs

Continuing the discussion from Workflow challenges with redefining structs (from "Why I still recommend Julia"):

I have decided to continue in a separate topic, but please see the linked post. So, I have now managed to make up a macro for “wrapping & re-defining”.

The usage is like following

@mwf module Moo
    
x1 = 1
x2 = 2

struct F7
    z
end

function f8(x)
    return x+8
end

end

Now you can use all variables defined within the module Moo directly in the Main scope:

julia> x1
1

If you want to redefine the struct F7 - just re-run the script.

The macro can also be applied directly to functions, structs and mutable structs like this:

@mwf struct Foo
    x
end

The effect would be the same as with

@mwf module ModModularWF_Foo
struct Foo
    x
end
end

For structs, it enables re-definition without restarting Julia. For functions, such a transparent wrapping into a module prevents inadvertent use of global variables within the function body and appearance of ghost methods (for which the source code doesn’t exist anymore).

For a bit more usage examples, see https://github.com/Eben60/ModularWF/blob/main/test/runtests.jl

Now the most exciting thing: such a re-definition of a struct using @mwf appears to function from within a Revise-ed module. It is just important that @mwf is already there before module loading. @tim.holy, what do you think about it?

The package, not (yet) registered, is on GitHub: GitHub - Eben60/ModularWF
As I have only minimal experience with metaprogramming, I would be very grateful if somebody more experienced looks onto my code.

4 Likes