Skip to content

Add ismutabletype #39037

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Dec 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 mutable 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The similar isstructtype and isprimitivetype definitions below seem to be at-pure; should this definition be as well? :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compiler should be able to prove that itself.

# 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