Skip to content

Commit 65184f9

Browse files
authored
Merge pull request #51 from SymbolicML/fix-fill-similar
Fix type stability of `fill_similar`
2 parents cd1fc0c + 118bcbf commit 65184f9

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DynamicExpressions"
22
uuid = "a40a106e-89c9-4ca8-8020-a735e8728b6b"
33
authors = ["MilesCranmer <[email protected]>"]
4-
version = "0.13.0"
4+
version = "0.13.1"
55

66
[deps]
77
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"

src/Utils.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ function _add_idmap_to_call(def::Expr, id_map::Expr)
155155
return Expr(:call, def.args[1], def.args[2:end]..., id_map)
156156
end
157157

158-
@inline fill_similar(value, array, args...) = fill!(similar(array, args...), value)
158+
@inline function fill_similar(value, array, args...)
159+
out_array = similar(array, args...)
160+
fill!(out_array, value)
161+
return out_array
162+
end
159163

160164
function deprecate_varmap(variable_names, varMap, func_name)
161165
if varMap !== nothing

test/test_utils.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using DynamicExpressions
2+
using DynamicExpressions.UtilsModule: fill_similar
3+
using Test
24

35
operators = OperatorEnum(; binary_operators=(+, *, -, /, ^), unary_operators=(exp, sin))
46

@@ -33,3 +35,8 @@ tree = x1 + Node(; val=0.0) - sin(x2 - Node(; val=0.5))
3335
@test get_constants(tree) == [0.0, 0.5]
3436
set_constants!(tree, [1.0, 2.0])
3537
@test repr(tree) == "((x1 + 1.0) - sin(x2 - 2.0))"
38+
39+
# Ensure that fill_similar is type stable
40+
x = randn(Float32, 3, 10)
41+
@inferred fill_similar(0.5f0, x, axes(x, 1))
42+
fill_similar(0.5f0, x, axes(x, 1)) == fill(0.5f0, 3)

0 commit comments

Comments
 (0)