Skip to content

Commit

Permalink
Add length check guards for delete methods (#31)
Browse files Browse the repository at this point in the history
* Add length check guards for delete methods

* add test

* Update test_ProtoStruct.jl

* Update test_ProtoStruct.jl

* Update test_ProtoStruct.jl

* Update test_ProtoStruct.jl

---------

Co-authored-by: Simon Christ <[email protected]>
Co-authored-by: Simon Christ <[email protected]>
  • Loading branch information
3 people authored Jan 12, 2024
1 parent e1124d9 commit df875e2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/ProtoStruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ macro proto(expr)
error("The supertype of a proto struct is not redefinable. Please restart your julia session.")
end
the_methods = collect(methods($name))
Base.delete_method(the_methods[1])
Base.delete_method(the_methods[2])
if length(the_methods) >= 1
Base.delete_method(the_methods[1])
end
if length(the_methods) >= 2
Base.delete_method(the_methods[2])
end
end

function $name($(fields...)) where {$(type_parameters...)}
Expand Down Expand Up @@ -181,8 +185,12 @@ macro proto(expr)
error("The supertype of a proto struct is not redefinable. Please restart your julia session.")
end
the_methods = collect(methods($name))
Base.delete_method(the_methods[1])
Base.delete_method(the_methods[2])
if length(the_methods) >= 1
Base.delete_method(the_methods[1])
end
if length(the_methods) >= 2
Base.delete_method(the_methods[2])
end
end

function $name($(fields...)) where {$(type_parameters...)}
Expand Down
15 changes: 15 additions & 0 deletions test/test_ProtoStruct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,21 @@ end
end
end

@proto struct TestMethods end

@testset "Constuctor updating I" begin
@test length(collect(methods(TestMethods))) == 1
end

@proto struct TestMethods
a
b
end

@testset "Constuctor updating II" begin
@test length(collect(methods(TestMethods))) == 2
end

"""
This is a docstring.
"""
Expand Down

0 comments on commit df875e2

Please sign in to comment.