Skip to content

Commit

Permalink
Fix type inference on earlier Julia
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Sep 21, 2023
1 parent 93802ff commit 242d33c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/BaseType.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ For example,
]
```
"""
function base_numeric_type(::Type{T}) where {T}
@generated function base_numeric_type(::Type{T}) where {T}
params = T isa UnionAll ? T.body.parameters : T.parameters
return isempty(params) ? T : first(params)
return isempty(params) ? :($T) : :($(first(params)))
end
base_numeric_type(x) = base_numeric_type(typeof(x))

Expand Down
8 changes: 2 additions & 6 deletions test/unittests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ expected_type_pairs = [
for (x, y) in expected_type_pairs
@eval @test base_numeric_type($x) == $y
# Make sure compiler can inline it:
if VERSION >= v"1.10"
@eval @inferred $y base_numeric_type($x)
end
@eval @inferred $y base_numeric_type($x)
end

@test base_numeric_type(1.5DynamicQuantities.u"km/s") == base_numeric_type(typeof(1.5DynamicQuantities.u"km/s"))
if VERSION >= v"1.10"
@inferred base_numeric_type(1.5DynamicQuantities.u"km/s")
end
@inferred base_numeric_type(1.5DynamicQuantities.u"km/s")

0 comments on commit 242d33c

Please sign in to comment.