Skip to content

Commit 480b0aa

Browse files
committed
Avoid conversion in Node initialization
1 parent 28f86cb commit 480b0aa

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/Equation.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ mutable struct Node{T}
4848
## Constructors:
4949
#################
5050
Node(d::Int, c::Bool, v::_T) where {_T} = new{_T}(d, c, v)
51+
Node(::Type{_T}, d::Int, c::Bool, v::_T) where {_T} = new{_T}(d, c, v)
5152
Node(::Type{_T}, d::Int, c::Bool, v::Nothing, f::Int) where {_T} = new{_T}(d, c, v, f)
5253
function Node(d::Int, c::Bool, v::Nothing, f::Int, o::Int, l::Node{_T}) where {_T}
5354
return new{_T}(d, c, v, f, o, l)
@@ -130,7 +131,17 @@ end
130131
function Node(
131132
::Type{T}; val::T1=nothing, feature::T2=nothing
132133
)::Node{T} where {T,T1,T2<:Union{Integer,Nothing}}
133-
return convert(Node{T}, Node(; val=val, feature=feature))
134+
if T1 <: Nothing && T2 <: Nothing
135+
error("You must specify either `val` or `feature` when creating a leaf node.")
136+
elseif !(T1 <: Nothing || T2 <: Nothing)
137+
error(
138+
"You must specify either `val` or `feature` when creating a leaf node, not both.",
139+
)
140+
elseif T2 <: Nothing
141+
return Node(T, 0, true, val)
142+
else
143+
return Node(T, 0, false, nothing, feature)
144+
end
134145
end
135146

136147
"""

0 commit comments

Comments
 (0)