Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

add isimmutabletype #18168

Closed
wants to merge 4 commits into from
Closed
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
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ This section lists changes that do not have deprecation warnings.
(since it is shorthand for `NTuple{N,T} where T`). To get the old behavior of matching
any tuple, use `NTuple{N,Any}` ([#18457]).

* `isimmutable(T::Type)` now checks whether `T` is an immutable type,
rather than checking whether `typeof(T)` is an immutable type ([#18168]).

* `FloatRange` has been replaced by `StepRangeLen`, and the internal
representation of `LinSpace` has changed. Aside from changes in
the internal field names, this leads to several differences in
Expand All @@ -156,7 +159,7 @@ This section lists changes that do not have deprecation warnings.
and `step::FloatNN` to rationals and construct a `StepRangeLen`
that internally uses twice-precision arithmetic. These two
outcomes exhibit differences in both precision and speed.

Copy link
Member

Choose a reason for hiding this comment

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

whitespace

Library improvements
--------------------

Expand Down Expand Up @@ -914,6 +917,7 @@ Language tooling improvements
[#17758]: https://github.com/JuliaLang/julia/issues/17758
[#17785]: https://github.com/JuliaLang/julia/issues/17785
[#18050]: https://github.com/JuliaLang/julia/issues/18050
[#18168]: https://github.com/JuliaLang/julia/issues/18168
[#18330]: https://github.com/JuliaLang/julia/issues/18330
[#18339]: https://github.com/JuliaLang/julia/issues/18339
[#18346]: https://github.com/JuliaLang/julia/issues/18346
Expand Down
10 changes: 6 additions & 4 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,15 @@ datatype_fielddesc_type(dt::DataType) = dt.layout == C_NULL ? throw(UndefRefErro
(unsafe_load(convert(Ptr{DataTypeLayout}, dt.layout)).alignment >> 30) & 3

"""
isimmutable(v)
isimmutable(x)

Return `true` iff value `v` is immutable. See [Immutable Composite Types](@ref)
for a discussion of immutability. Note that this function works on values, so if you give it
a type, it will tell you that a value of `DataType` is mutable.
Return `true` if the value `x` is immutable; if `x` is a type, then returns
whether `x` is an immutable type. See [Immutable Composite Types](@ref)
for a discussion of immutability.
"""
isimmutable(x::ANY) = (@_pure_meta; (isa(x,Tuple) || !typeof(x).mutable))
isimmutable(t::DataType) = (@_pure_meta; !t.mutable)
isimmutable(::Type) = (@_pure_meta; false)
isstructtype(t::DataType) = (@_pure_meta; nfields(t) != 0 || (t.size==0 && !t.abstract))
isstructtype(x) = (@_pure_meta; false)

Expand Down
9 changes: 9 additions & 0 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ not_const = 1

@test isimmutable(1) == true
@test isimmutable([]) == false
@test isimmutable((3,4,5)) == true
@test isimmutable(Int) == true
@test isimmutable(Vector{Int}) == false
@test isimmutable(Tuple{Int}) == true

# should String in 0.6 be immutable?
@test_broken isimmutable("abc") == true
@test_broken isimmutable(String) == true


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