Skip to content

Commit 575f067

Browse files
authored
Move unionall_type to Utilities, add docs (#2238)
This PR moves the two identical instances of `unionall_type` (and the similarly named `union_all_type`) to a common function in `Utilities`. It also adds examples to clarify what the function does.
1 parent e020b17 commit 575f067

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/DataLayouts/DataLayouts.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import ClimaComms
6969
import MultiBroadcastFusion as MBF
7070
import Adapt
7171

72+
import ..Utilities: PlusHalf, unionall_type
7273
import ..DebugOnly: call_post_op_callback, post_op_callback
7374
import ..slab, ..slab_args, ..column, ..column_args, ..level
7475
export slab,
@@ -2335,10 +2336,8 @@ function ColumnMask(
23352336
return IJHMask(is_active)
23362337
end
23372338

2338-
union_all_type(::Type{T}) where {T} = T.name.wrapper
2339-
23402339
function IJHMask(is_active::Union{IJFH, IJHF})
2341-
DA = union_all_type(typeof(parent(is_active)))
2340+
DA = unionall_type(typeof(parent(is_active)))
23422341
(Ni, Nj, _, _, Nh) = size(is_active)
23432342
Nijh = Ni * Nj * Nh
23442343
i_map = zeros(Int, Nijh)

src/Operators/finitedifference.jl

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ..Utilities: PlusHalf, half
1+
import ..Utilities: PlusHalf, half, unionall_type
22
import UnrolledUtilities: unrolled_map
33

44
const AllFiniteDifferenceSpace =
@@ -3296,13 +3296,6 @@ Base.@propagate_inbounds function stencil_right_boundary(
32963296
stencil_interior(op, loc, space, idx - 1, hidx, arg)
32973297
end
32983298

3299-
"""
3300-
unionall_type(::Type{T})
3301-
3302-
Extract the type of the input, and strip it of any type parameters.
3303-
"""
3304-
unionall_type(::Type{T}) where {T} = T.name.wrapper
3305-
33063299
# Extend `adapt_structure` for all boundary conditions containing a `val` field.
33073300
function Adapt.adapt_structure(to, bc::AbstractBoundaryCondition)
33083301
if hasfield(typeof(bc), :val)

src/Utilities/Utilities.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,35 @@ Base.@propagate_inbounds linear_ind(n::NTuple, ci::CartesianIndex) =
3030
Base.@propagate_inbounds linear_ind(n::NTuple, loc::NTuple) =
3131
linear_ind(n, CartesianIndex(loc))
3232

33+
"""
34+
unionall_type(::Type{T})
35+
36+
Extract the type of the input, and strip it of any type parameters.
37+
38+
This is useful when one needs the generic constructor for a given type.
39+
40+
Example
41+
=======
42+
```julia
43+
julia> unionall_type(typeof([1, 2, 3]))
44+
Array
45+
46+
julia> struct Foo{A, B}
47+
a::A
48+
b::B
49+
end
50+
51+
julia> unionall_type(typeof(Foo(1,2)))
52+
Foo
53+
```
54+
"""
55+
function unionall_type(::Type{T}) where {T}
56+
# NOTE: As of version 1.12, there is no simple, user-friendly way to extract
57+
# the generic type of T, so we need to reach for the internals in Julia.
58+
# Hopefully, Julia will introduce a simpler, more stable way to do this in a
59+
# future release.
60+
return T.name.wrapper
61+
end
62+
63+
3364
end # module

0 commit comments

Comments
 (0)