Skip to content

Commit

Permalink
Type stability for variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Aug 2, 2023
1 parent d505a2f commit 082d5b9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/OperatorEnumConstruction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const ALREADY_DEFINED_UNARY_OPERATORS = (;
const ALREADY_DEFINED_BINARY_OPERATORS = (;
operator_enum=Dict{Function,Bool}(), generic_operator_enum=Dict{Function,Bool}()
)
const LATEST_VARIABLE_NAMES = Ref{Union{Nothing,Vector{String}}}(nothing)
const LATEST_VARIABLE_NAMES = Ref{Vector{String}}(String[])

function Base.show(io::IO, tree::Node)
latest_operators_type = LATEST_OPERATORS_TYPE.x
Expand Down
14 changes: 9 additions & 5 deletions test/test_print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,18 @@ end
end

@testset "Test variable names" begin
operators = OperatorEnum(;
binary_operators=[+, *, /, -], unary_operators=[cos, sin],
)
operators = OperatorEnum(; binary_operators=[+, *, /, -], unary_operators=[cos, sin])
@extend_operators operators
x1, x2, x3 = [Node(Float64; feature=i) for i in 1:3]
DynamicExpressions.OperatorEnumConstructionModule.LATEST_VARIABLE_NAMES.x = ["k1", "k2", "k3"]
DynamicExpressions.OperatorEnumConstructionModule.LATEST_VARIABLE_NAMES.x = [
"k1", "k2", "k3"
]
tree = x1 * x2 + x3
@test string(tree) == "((k1 * k2) + k3)"
DynamicExpressions.OperatorEnumConstructionModule.LATEST_VARIABLE_NAMES.x = nothing
empty!(DynamicExpressions.OperatorEnumConstructionModule.LATEST_VARIABLE_NAMES.x)
@test string(tree) == "((x1 * x2) + x3)"
# Check if we can pass the wrong number of variable names:
DynamicExpressions.OperatorEnumConstructionModule.LATEST_VARIABLE_NAMES.x = ["k1"]
@test string(tree) == "((k1 * x2) + x3)"
empty!(DynamicExpressions.OperatorEnumConstructionModule.LATEST_VARIABLE_NAMES.x)
end

0 comments on commit 082d5b9

Please sign in to comment.