|
1 | 1 | module TypeUtils |
2 | 2 |
|
3 | 3 | export |
| 4 | + @assert_floating_point, |
4 | 5 | ArrayAxes, |
5 | 6 | ArrayAxis, |
6 | 7 | ArrayShape, |
|
15 | 16 | as_array_size, |
16 | 17 | as_eltype, |
17 | 18 | as_return, |
| 19 | + assert_floating_point, |
18 | 20 | bare_type, |
19 | 21 | convert_bare_type, |
20 | 22 | convert_eltype, |
@@ -54,6 +56,22 @@ macro public(args::Union{Symbol,Expr}...) |
54 | 56 | end |
55 | 57 | VERSION ≥ v"1.11.0-DEV.469" && eval(Expr(:public, Symbol("@public"))) |
56 | 58 |
|
| 59 | +""" |
| 60 | + @assert_floating_point A B ... |
| 61 | +
|
| 62 | +throws an `ArgumentError` exception if any of the variables `A`, `B`, etc. does not use |
| 63 | +floating-point to store its value(s). |
| 64 | +
|
| 65 | +See also [`assert_floating_point`](@ref). |
| 66 | +
|
| 67 | +""" |
| 68 | +macro assert_floating_point(args::Symbol...) |
| 69 | + code = [:(assert_floating_point($(QuoteNode(arg)), $(esc(arg)))) for arg in args] |
| 70 | + return quote |
| 71 | + $(code...) |
| 72 | + end |
| 73 | +end |
| 74 | + |
57 | 75 | """ |
58 | 76 | c = TypeUtils.Converter(f, T::Type) |
59 | 77 |
|
@@ -878,6 +896,52 @@ for any `x`. |
878 | 896 | convert_floating_point_type(::Type{T}) where {T} = |
879 | 897 | Converter(convert_floating_point_type, floating_point_type(T)) |
880 | 898 |
|
| 899 | +""" |
| 900 | + assert_floating_point(Bool, x) -> bool |
| 901 | +
|
| 902 | +yields whether `x` uses floating-point to store its value(s). For n-tuples, the same |
| 903 | +floating-point type must be used for all values. |
| 904 | +
|
| 905 | +""" |
| 906 | +assert_floating_point(::Type{Bool}, x) = |
| 907 | + assert_floating_point(Bool, typeof(x)) |
| 908 | + |
| 909 | +assert_floating_point(::Type{Bool}, ::Type{<:AbstractArray{T}}) where {T} = |
| 910 | + assert_floating_point(Bool, T) |
| 911 | + |
| 912 | +assert_floating_point(::Type{Bool}, ::Type{<:NTuple{N,T}}) where {N,T} = |
| 913 | + assert_floating_point(Bool, T) |
| 914 | + |
| 915 | +assert_floating_point(::Type{Bool}, ::DataType) = false |
| 916 | + |
| 917 | +assert_floating_point(::Type{Bool}, ::Type{T}) where {T<:Number} = |
| 918 | + _assert_floating_point(Bool, real_type(T)) |
| 919 | + |
| 920 | +_assert_floating_point(::Type{Bool}, ::Type{T}) where {T<:AbstractFloat} = isconcretetype(T) |
| 921 | +_assert_floating_point(::Type{Bool}, ::Type{T}) where {T<:Number} = false |
| 922 | + |
| 923 | +""" |
| 924 | + assert_floating_point([name,] x) |
| 925 | +
|
| 926 | +throws an exception if `x` does not use floating-point to store its value(s). Optional |
| 927 | +`name` argument is to specify the name to use for the error message. |
| 928 | +
|
| 929 | +See also [`@assert_floating_point`](@ref). |
| 930 | +
|
| 931 | +""" |
| 932 | +assert_floating_point(x) = assert_floating_point(typeof(x)) |
| 933 | +assert_floating_point(::Type{T}) where {T} = |
| 934 | + assert_floating_point(Bool, T) ? nothing : throw_not_floating_point(T) |
| 935 | + |
| 936 | +assert_floating_point(name::Union{Symbol,AbstractString}, x) = assert_floating_point(name, typeof(x)) |
| 937 | +assert_floating_point(name::Union{Symbol,AbstractString}, ::Type{T}) where {T} = |
| 938 | + assert_floating_point(Bool, T) ? nothing : throw_not_floating_point(name, T) |
| 939 | + |
| 940 | +@noinline throw_not_floating_point(::Type{T}) where {T} = |
| 941 | + throw(ArgumentError("type `$T` is not floating-point")) |
| 942 | +@noinline throw_not_floating_point(name::Union{Symbol,AbstractString}, ::Type{T}) where {T} = |
| 943 | + throw(ArgumentError("argument or variable `$name` of type `$T` is not floating-point")) |
| 944 | + |
881 | 945 | """ |
882 | 946 | promote_eltype(args...) |
883 | 947 |
|
|
0 commit comments