Skip to content

Commit 13859b7

Browse files
committed
add isimmutabletype
1 parent 571aad6 commit 13859b7

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ Library improvements
210210

211211
* New `iszero(x)` function to quickly check whether `x` is zero (or is all zeros, for an array) ([#19950]).
212212

213+
* New function `isimmutabletype(T)` ([#18168]).
214+
213215
* `notify` now returns a count of tasks woken up ([#19841]).
214216

215217
Compiler/Runtime improvements
@@ -883,6 +885,7 @@ Language tooling improvements
883885
[#17758]: https://github.com/JuliaLang/julia/issues/17758
884886
[#17785]: https://github.com/JuliaLang/julia/issues/17785
885887
[#18050]: https://github.com/JuliaLang/julia/issues/18050
888+
[#18168]: https://github.com/JuliaLang/julia/issues/18168
886889
[#18330]: https://github.com/JuliaLang/julia/issues/18330
887890
[#18339]: https://github.com/JuliaLang/julia/issues/18339
888891
[#18346]: https://github.com/JuliaLang/julia/issues/18346
@@ -919,4 +922,5 @@ Language tooling improvements
919922
[#19919]: https://github.com/JuliaLang/julia/issues/19919
920923
[#19944]: https://github.com/JuliaLang/julia/issues/19944
921924
[#19950]: https://github.com/JuliaLang/julia/issues/19950
925+
[#20079]: https://github.com/JuliaLang/julia/issues/20079
922926
[#20164]: https://github.com/JuliaLang/julia/issues/20164

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ export
946946
isbits,
947947
isequal,
948948
isimmutable,
949+
isimmutabletype,
949950
isless,
950951
ifelse,
951952
lexless,

base/reflection.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ isimmutable(x::ANY) = (@_pure_meta; (isa(x,Tuple) || !typeof(x).mutable))
205205
isstructtype(t::DataType) = (@_pure_meta; nfields(t) != 0 || (t.size==0 && !t.abstract))
206206
isstructtype(x) = (@_pure_meta; false)
207207

208+
"""
209+
isimmutabletype(T)
210+
211+
Return `true` if the type `T` is immutable. See [manual](:ref:`man-immutable-composite-types`)
212+
for a discussion of immutability. See also [`isimmutable`](:func:`isimmutable`)
213+
for the corresponding function acting on values rather than types.
214+
"""
215+
isimmutabletype(t::ANY) = (@_pure_meta; isa(t, DataType) && !t.mutable)
216+
208217
"""
209218
isbits(T)
210219

test/reflection.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ not_const = 1
159159

160160
@test isimmutable(1) == true
161161
@test isimmutable([]) == false
162+
@test isimmutable("abc") == true
163+
@test isimmutable((3,4,5)) == true
164+
@test isimmutabletype(Int) == true
165+
@test isimmutabletype(Vector{Int}) == false
166+
@test isimmutabletype(String) == true
167+
@test isimmutabletype(Tuple{Int}) == true
168+
162169

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

0 commit comments

Comments
 (0)