Workflow challenges with redefining structs (from "Why I still recommend Julia")

That’s what the

Does. It is useful in early development stages.

6 Likes

By the way, ProtoStructs are incompatible with StructTypes:

using JSON3
using StructTypes
using ProtoStructs

json_string = """{"a": 1, "b": "hello, world"}"""

@proto struct MyType1
    a::Int
    b::String
end

StructTypes.StructType(::Type{MyType1}) = StructTypes.Struct() # error

hello_world = JSON3.read(json_string, MyType1)

println(hello_world)

JSON3.write(hello_world)

Sometimes I use a workflow similar to what @Ralph_Smith suggested (instead of or in addition to the Revise-based workflow).

module MM

a = 1
b = 2
foo(x) = x + 1
struct Coord
	x
	y
end

end

a = MM.a
b = MM.b
Coord = MM.Coord
foo = MM.foo

c = Coord(a, foo(b))
julia> 
WARNING: replacing module MM.
Main.MM.Coord(1, 3)

After any changes in the module just re-run the script. One of the reasons to use such workflow is the possibility to re-define structs. Another one - I may start with calculations in the global scope and move them into functions bit by bit.

An annoying aspect of the approach is that either I have to always prefix my a, b, foo, Coord with the module name, or define variables in the Main scope as in the example above.

Now I went a step further and automated the Main scope variables binding:

module DraftsModule

a = 1
b = 2
foo(x) = x + 1
struct Coord
	x
	y
end

allnames = names(DraftsModule; all=true)
end # DraftsModule

for n in DraftsModule.allnames
    if Base.isidentifier(n) && n ∉ (:DraftsModule, :eval, :include)
        eval(Meta.parse("$n = DraftsModule.$n"))
    end
end

Herewith I just have the following code saved as a template:

module DraftsModule

# put your code here

allnames = names(DraftsModule; all=true)
end # DraftsModule

for n in DraftsModule.allnames
    if Base.isidentifier(n) && n ∉ (:DraftsModule, :eval, :include)
        eval(Meta.parse("$n = DraftsModule.$n"))
    end
end
# work with the variables defined in DraftsModule

I can imagine putting this code into a macro and use it like following, but that appears beyond my competence.

@do_it_somehow begin
module MyModule
#put your code here
end #module
end #macro
# work with the variables defined in MyModule
1 Like