Skip to content

Commit e8ff535

Browse files
committed
Use keyword constructor in other parts of codebase
1 parent a75a26c commit e8ff535

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

ext/DynamicExpressionsSymbolicUtilsExt.jl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ function split_eq(
9393
else
9494
ind = findoperation(op, operators.binops)
9595
end
96-
return constructorof(N)(
97-
ind,
98-
convert(N, args[1], operators; variable_names=variable_names),
99-
convert(N, op(args[2:end]...), operators; variable_names=variable_names),
96+
return constructorof(N)(;
97+
op=ind,
98+
l=convert(N, args[1], operators; variable_names=variable_names),
99+
r=convert(N, op(args[2:end]...), operators; variable_names=variable_names),
100100
)
101101
end
102102

@@ -157,8 +157,9 @@ function Base.convert(
157157
findoperation(op, operators.unaops)
158158
end
159159

160-
return constructorof(N)(
161-
ind, map(x -> convert(N, x, operators; variable_names=variable_names), args)...
160+
return constructorof(N)(;
161+
op=ind,
162+
children=map(x -> convert(N, x, operators; variable_names=variable_names), args),
162163
)
163164
end
164165

src/Equation.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,16 @@ nodes, you can evaluate or print a given expression.
8080
# Constructors
8181
8282
83-
Node([T]; val=nothing, feature=nothing, op=nothing, l=nothing, r=nothing, allocator=default_allocator)
84-
Node{T}(; val=nothing, feature=nothing, op=nothing, l=nothing, r=nothing, allocator=default_allocator)
83+
Node([T]; val=nothing, feature=nothing, op=nothing, l=nothing, r=nothing, children=nothing, allocator=default_allocator)
84+
Node{T}(; val=nothing, feature=nothing, op=nothing, l=nothing, r=nothing, children=nothing, allocator=default_allocator)
8585
8686
Create a new node in an expression tree. If `T` is not specified in either the type or the
8787
first argument, it will be inferred from the value of `val` passed or `l` and/or `r`.
8888
If it cannot be inferred from these, it will default to `Float32`.
8989
90+
The `children` keyword can be used instead of `l` and `r` and should be a tuple of children. This
91+
is to permit the use of splatting in constructors.
92+
9093
You may also construct nodes via the convenience operators generated by creating an `OperatorEnum`.
9194
9295
You may also choose to specify a default memory allocator for the node other than simply `Node{T}()`
@@ -183,8 +186,12 @@ include("base.jl")
183186

184187
#! format: off
185188
@inline function (::Type{N})(
186-
::Type{T1}=Undefined; val=nothing, feature=nothing, op=nothing, l=nothing, r=nothing, allocator=default_allocator,
189+
::Type{T1}=Undefined; val=nothing, feature=nothing, op=nothing, l=nothing, r=nothing, children=nothing, allocator=default_allocator,
187190
) where {T1,N<:AbstractExpressionNode}
191+
if children !== nothing
192+
@assert l === nothing && r === nothing
193+
return node_factory(N, T1, val, feature, op, children..., allocator)
194+
end
188195
return node_factory(N, T1, val, feature, op, l, r, allocator)
189196
end
190197
"""Create a constant leaf."""

src/OperatorEnumConstruction.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function _extend_unary_operator(f::Symbol, type_requirements, internal)
125125
$_constructorof(N)(T; val=$($f)(l.val::T))
126126
else
127127
latest_op_idx = $($lookup_op)($($f), Val(1))
128-
$_constructorof(N)(latest_op_idx, l)
128+
$_constructorof(N)(; op=latest_op_idx, l=l)
129129
end
130130
end
131131
end
@@ -152,7 +152,7 @@ function _extend_binary_operator(f::Symbol, type_requirements, build_converters,
152152
$_constructorof(N)(T; val=$($f)(l.val::T, r.val::T))
153153
else
154154
latest_op_idx = $($lookup_op)($($f), Val(2))
155-
$_constructorof(N)(latest_op_idx, l, r)
155+
$_constructorof(N)(; op=latest_op_idx, l=l, r=r)
156156
end
157157
end
158158
function $($f)(
@@ -162,7 +162,9 @@ function _extend_binary_operator(f::Symbol, type_requirements, build_converters,
162162
$_constructorof(N)(T; val=$($f)(l.val::T, r))
163163
else
164164
latest_op_idx = $($lookup_op)($($f), Val(2))
165-
$_constructorof(N)(latest_op_idx, l, $_constructorof(N)(T; val=r))
165+
$_constructorof(N)(;
166+
op=latest_op_idx, l=l, r=$_constructorof(N)(T; val=r)
167+
)
166168
end
167169
end
168170
function $($f)(
@@ -172,7 +174,9 @@ function _extend_binary_operator(f::Symbol, type_requirements, build_converters,
172174
$_constructorof(N)(T; val=$($f)(l, r.val::T))
173175
else
174176
latest_op_idx = $($lookup_op)($($f), Val(2))
175-
$_constructorof(N)(latest_op_idx, $_constructorof(N)(T; val=l), r)
177+
$_constructorof(N)(;
178+
op=latest_op_idx, l=$_constructorof(N)(T; val=l), r=r
179+
)
176180
end
177181
end
178182
if $($build_converters)

src/base.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ function copy_node(
440440
constructorof(N)(T; feature=t.feature)
441441
end,
442442
identity,
443-
(p, c...) -> constructorof(N)(p.op, c...),
443+
(p, children...) -> constructorof(N)(; op=p.op, children),
444444
tree,
445445
N;
446446
break_sharing,
@@ -483,7 +483,7 @@ function convert(
483483
constructorof(N1)(T1; feature=t.feature)
484484
end,
485485
identity,
486-
((p, c::Vararg{Any,M}) where {M}) -> constructorof(N1)(p.op, c...),
486+
(p, children...) -> constructorof(N1)(; op=p.op, children),
487487
tree,
488488
N1,
489489
)

0 commit comments

Comments
 (0)