Skip to content

Commit 69fd65e

Browse files
committed
Improve readability
1 parent a575e0b commit 69fd65e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/BaseType.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ base_numeric_type(x) = base_numeric_type(typeof(x))
3030

3131
function _base_numeric_type(::Type{T}) where {T}
3232
params = T isa UnionAll ? T.body.parameters : T.parameters
33-
return isempty(params) ? T : _base_numeric_type(first(params))
34-
# TODO: deal with recursive types
33+
if isempty(params)
34+
return T
35+
else
36+
return _base_numeric_type(first(params))
37+
end
3538
end
3639

3740
end

test/unittests.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Test: @test, @testset, @inferred
22
using BaseType: base_numeric_type
3-
using DualNumbers: DualNumbers
3+
using DualNumbers: DualNumbers, Dual
44
using DynamicQuantities: DynamicQuantities
55
using Measurements: ±
66
using Unitful: Unitful
@@ -37,3 +37,13 @@ end
3737
y = Dual(1.0)Unitful.u"m/s"
3838
@test base_numeric_type(y) == Float64
3939
end
40+
41+
struct Node{T}
42+
child::Union{Node{T},Nothing}
43+
value::T
44+
end
45+
46+
@testset "Safe default behavior for recursive types" begin
47+
c = Node{Int}(Node{Int}(nothing, 1), 2)
48+
@test base_numeric_type(c) == Int
49+
end

0 commit comments

Comments
 (0)