Skip to content

Commit

Permalink
Discourage use on containers
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer authored Sep 21, 2023
1 parent 261f72c commit 95bf91b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 22 deletions.
3 changes: 1 addition & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
DualNumbers = "fa6b7ba4-c1ee-5f82-b5fc-ecf0adba8f74"
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
Measurements = "eff96d63-e80a-5855-80a2-b1b0885c5ab7"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"

[targets]
test = ["Aqua", "DualNumbers", "DynamicQuantities", "Measurements", "StaticArrays", "Test", "Unitful"]
test = ["Aqua", "DualNumbers", "DynamicQuantities", "Measurements", "Test", "Unitful"]
17 changes: 8 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Aqua](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl)

This package exports a tiny function `base_numeric_type` that
extracts the base numeric type from a possible container type `T`:
extracts the base numeric type from a numeric type `T`:

- `base_numeric_type(::Type{T}) where {T}`
- `base_numeric_type(x::T)`
Expand All @@ -16,18 +16,17 @@ For example,
|---|---|
| `Float32` | `Float32` |
| `ComplexF32` | `Float32` |
| `Array{ComplexF32,1}` | `ComplexF32` |
| `Set{Float32}` | `Float32` |
| `Measurement{Float32}` | `Float32` |
| `Dual{BigFloat}` | `BigFloat` |
| `Rational{Int8}` | `Int8` |
| `Quantity{Float32,Dimensions}` | `Float32` |

Packages should write a method to `base_numeric_type`
when the base numeric type of a container type
is not the first parametric type. Otherwise,
the default method will already be valid.
when the base type of a numeric type
is not the first parametric type.
For example, if you were to create a quantity-like type
`Quantity{Dimensions,NumericType}`, you would need
to write a custom interface.

Furthermore, this is not needed for container types
that inherit from `AbstractArray{T}`, as `T`
will be taken as the base numeric type.
But if the base type comes first,
the default method will work.
8 changes: 2 additions & 6 deletions src/BaseType.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ export base_numeric_type
base_numeric_type(::Type{T}) where {T}
base_numeric_type(x::T)
Extract the base numeric type from a possible container type `T`.
Extract the base numeric type from a numerical type `T` such
as a measurement or a quantity.
For example,
| Input Type | Output Type |
|---|---|
| `Float32` | `Float32` |
| `ComplexF32` | `Float32` |
| `Array{ComplexF32,1}` | `ComplexF32` |
| `Set{Float32}` | `Float32` |
| `Measurement{Float32}` | `Float32` |
| `Rational{Int8}` | `Int8` |
| `Dual{BigFloat}` | `BigFloat` |
Expand All @@ -27,7 +26,4 @@ For example,
end
base_numeric_type(x) = base_numeric_type(typeof(x))

# Special cases:
base_numeric_type(::Type{<:AbstractArray{T}}) where {T} = T

end
5 changes: 0 additions & 5 deletions test/unittests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@ using BaseType: base_numeric_type
using DualNumbers: DualNumbers
using DynamicQuantities: DynamicQuantities
using Measurements: ±
using StaticArrays: StaticArrays
using Unitful: Unitful

expected_type_pairs = [
Float32 => Float32,
Array{Float64,1} => Float64,
ComplexF64 => Float64,
Matrix{ComplexF64} => ComplexF64,
DualNumbers.Dual{Int64} => Int64,
DynamicQuantities.Quantity{Float32} => Float32,
typeof(1.5DynamicQuantities.u"km/s") => Float64,
typeof(1.5f0Unitful.u"km/s") => Float32,
typeof(StaticArrays.SArray{Tuple{32,3}}(randn(32, 3))) => Float64,
typeof(StaticArrays.SArray{Tuple{32,3}}(randn(Float32, 32, 3))) => Float32,
BigFloat => BigFloat,
typeof(1.5 ± 0.2) => Float64,
typeof(1.5f0 ± 0.2f0) => Float32,
Expand Down

0 comments on commit 95bf91b

Please sign in to comment.