Skip to content

Commit c850e97

Browse files
committed
fix: type instability in string_tree
1 parent 4b5d681 commit c850e97

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/Strings.jl

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@ const OP_NAMES = Base.ImmutableDict(
1414
"safe_pow" => "^",
1515
)
1616

17-
function dispatch_op_name(::Val{2}, ::Nothing, idx)::Vector{Char}
18-
return vcat(collect("binary_operator["), collect(string(idx)), [']'])
19-
end
20-
function dispatch_op_name(::Val{1}, ::Nothing, idx)::Vector{Char}
21-
return vcat(collect("unary_operator["), collect(string(idx)), [']'])
22-
end
23-
function dispatch_op_name(::Val{2}, operators::AbstractOperatorEnum, idx)::Vector{Char}
24-
return get_op_name(operators.binops[idx])
17+
function dispatch_op_name(::Val{deg}, ::Nothing, idx)::Vector{Char} where {deg}
18+
if deg == 1
19+
return vcat(collect("unary_operator["), collect(string(idx)), [']'])
20+
else
21+
return vcat(collect("binary_operator["), collect(string(idx)), [']'])
22+
end
2523
end
26-
function dispatch_op_name(::Val{1}, operators::AbstractOperatorEnum, idx)::Vector{Char}
27-
return get_op_name(operators.unaops[idx])
24+
function dispatch_op_name(::Val{deg}, operators::AbstractOperatorEnum, idx) where {deg}
25+
if deg == 1
26+
return get_op_name(operators.unaops[idx])::Vector{Char}
27+
else
28+
return get_op_name(operators.binops[idx])::Vector{Char}
29+
end
2830
end
2931

3032
@generated function get_op_name(op::F)::Vector{Char} where {F}
@@ -137,15 +139,22 @@ function string_tree(
137139
)::String where {T,F1<:Function,F2<:Function}
138140
variable_names = deprecate_varmap(variable_names, varMap, :string_tree)
139141
raw_output = tree_mapreduce(
140-
leaf -> if leaf.constant
141-
collect(f_constant(leaf.val))
142-
else
143-
collect(f_variable(leaf.feature, variable_names))
142+
let f_constant = f_constant,
143+
f_variable = f_variable,
144+
variable_names = variable_names
145+
146+
(leaf,) -> if leaf.constant
147+
collect(f_constant(leaf.val))::Vector{Char}
148+
else
149+
collect(f_variable(leaf.feature, variable_names))::Vector{Char}
150+
end
144151
end,
145-
branch -> if branch.degree == 1
146-
dispatch_op_name(Val(1), operators, branch.op)
147-
else
148-
dispatch_op_name(Val(2), operators, branch.op)
152+
let operators = operators
153+
(branch,) -> if branch.degree == 1
154+
dispatch_op_name(Val(1), operators, branch.op)::Vector{Char}
155+
else
156+
dispatch_op_name(Val(2), operators, branch.op)::Vector{Char}
157+
end
149158
end,
150159
combine_op_with_inputs,
151160
tree,

0 commit comments

Comments
 (0)