Skip to content

Commit e66ef7f

Browse files
committed
Add ismutabletype
Determines whether a type was declared using `mutable struct`. Naming follows from the `ismutable` query we already have, analogous to `isbits`/`isbitstype`. Replaces #18168.
1 parent ad7e59a commit e66ef7f

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ export
656656
isbits,
657657
isequal,
658658
ismutable,
659+
ismutabletype,
659660
isless,
660661
isunordered,
661662
ifelse,

base/reflection.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,23 @@ true
460460
"""
461461
ismutable(@nospecialize(x)) = (@_pure_meta; typeof(x).mutable)
462462

463+
464+
"""
465+
ismutabletype(T) -> Bool
466+
467+
Determine whether type `T` was declared as a mutable type
468+
(i.e. using `mutable struct` keyword).
469+
470+
!!! compat "Julia 1.7"
471+
This function requires at least Julia 1.7.
472+
"""
473+
function ismutabletype(@nospecialize(t::Type))
474+
t = unwrap_unionall(t)
475+
# TODO: what to do for `Union`?
476+
return isa(t, DataType) && t.mutable
477+
end
478+
479+
463480
"""
464481
isstructtype(T) -> Bool
465482

test/reflection.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ not_const = 1
113113

114114
@test ismutable(1) == false
115115
@test ismutable([]) == true
116+
@test ismutabletype(Int) == false
117+
@test ismutabletype(Vector{Any}) == true
118+
@test ismutabletype(Union{Int, Vector{Any}}) == false
116119

117120
## find bindings tests
118121
@test ccall(:jl_get_module_of_binding, Any, (Any, Any), Base, :sin)==Base

0 commit comments

Comments
 (0)