Skip to content

Commit

Permalink
Add ismutabletype
Browse files Browse the repository at this point in the history
Determines whether a type was declared using `mutable struct`.
Naming follows from the `ismutable` query we already have, analogous
to `isbits`/`isbitstype`. Replaces #18168.
  • Loading branch information
Keno committed Dec 30, 2020
1 parent ad7e59a commit d07adb1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ export
isbits,
isequal,
ismutable,
ismutabletype,
isless,
isunordered,
ifelse,
Expand Down
17 changes: 17 additions & 0 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,23 @@ true
"""
ismutable(@nospecialize(x)) = (@_pure_meta; typeof(x).mutable)


"""
ismutabletype(T) -> Bool
Determine whether type `T` was declared as a struct type
(i.e. using `mutable struct` keyword).
!!! compat "Julia 1.7"
This function requires at least Julia 1.7.
"""
function ismutabletype(@nospecialize(t::Type))
t = unwrap_unionall(t)
# TODO: what to do for `Union`?
return isa(t, DataType) && t.mutable
end


"""
isstructtype(T) -> Bool
Expand Down
3 changes: 3 additions & 0 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ not_const = 1

@test ismutable(1) == false
@test ismutable([]) == true
@test ismutabletype(Int) == false
@test ismutabletype(Vector{Any}) == true
@test ismutabletype(Union{Int, Vector{Any}}) == false

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

0 comments on commit d07adb1

Please sign in to comment.