如何检查类型的可变性
How to check a type for mutability
有没有办法检查一个类型是可变的还是不可变的?是否可以在编译时完成此检查(即让分支 if ismutable(T)
编译掉以仅使用代码路径来实现可变性或不变性)?
DataTypes
有一个 mutable
字段,因此您可以使用它定义 is_mutable
和 is_immutable
,因为这样做而不是直接访问该字段更像 Julian。
使用 Julia 版本 0.5.0:
_
_ _ _(_)_ | By greedy hackers for greedy hackers.
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _' | |
| | |_| | | | (_| | | Version 0.5.0 (2016-09-19 18:14 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-w64-mingw32
julia> DataType. # <TAB> to auto complete
abstract hastypevars instance layout llvm::StructType name parameters super uid
depth haswildcard isleaftype llvm::DIType mutable ninitialized size types
julia> is_mutable(x::DataType) = x.mutable
is_mutable (generic function with 1 method)
julia> is_mutable(x) = is_mutable(typeof(x))
is_mutable (generic function with 2 methods)
julia> is_immutable(x) = !is_mutable(x)
is_immutable (generic function with 1 method)
创建一个 type
和一个 immutable
以及它们的实例:
julia> type Foo end
julia> f = Foo()
Foo()
julia> immutable Bar end
julia> b = Bar()
Bar()
检查可变性:
julia> is_mutable(Foo), is_mutable(f)
(true,true)
julia> is_mutable(Bar), is_mutable(b)
(false,false)
检查不变性:
julia> is_immutable(Foo), is_immutable(f)
(false,false)
julia> is_immutable(Bar), is_immutable(b)
(true,true)
为了性能,还可以考虑将这些函数声明为 @pure
:
Base.@pure is_mutable(x::DataType) = x.mutable
从 julia 1.7 开始,有一个新的标准库函数 ismutabletype
。如果你真的想要 ismutable
处理类型,你可以为它添加另一个方法,如下所示:
function ismutable(t::Type) ::Bool
return ismutabletype(t)
end
用法:
mutable struct MutableType end
struct ImmutableType end
mutable_instance = MutableType()
immutable_instance = ImmutableType()
ismutable(mutable_instance) # true
ismutable(typeof(mutable_instance)) # true
ismutable(immutable_instance) # false
ismutable(typeof(immutable_instance)) # false
我只推荐使用 Base.ismutabletype
开始,在大多数情况下不推荐向标准库函数添加方法
有没有办法检查一个类型是可变的还是不可变的?是否可以在编译时完成此检查(即让分支 if ismutable(T)
编译掉以仅使用代码路径来实现可变性或不变性)?
DataTypes
有一个 mutable
字段,因此您可以使用它定义 is_mutable
和 is_immutable
,因为这样做而不是直接访问该字段更像 Julian。
使用 Julia 版本 0.5.0:
_
_ _ _(_)_ | By greedy hackers for greedy hackers.
(_) | (_) (_) | Documentation: http://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _' | |
| | |_| | | | (_| | | Version 0.5.0 (2016-09-19 18:14 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-w64-mingw32
julia> DataType. # <TAB> to auto complete
abstract hastypevars instance layout llvm::StructType name parameters super uid
depth haswildcard isleaftype llvm::DIType mutable ninitialized size types
julia> is_mutable(x::DataType) = x.mutable
is_mutable (generic function with 1 method)
julia> is_mutable(x) = is_mutable(typeof(x))
is_mutable (generic function with 2 methods)
julia> is_immutable(x) = !is_mutable(x)
is_immutable (generic function with 1 method)
创建一个 type
和一个 immutable
以及它们的实例:
julia> type Foo end
julia> f = Foo()
Foo()
julia> immutable Bar end
julia> b = Bar()
Bar()
检查可变性:
julia> is_mutable(Foo), is_mutable(f)
(true,true)
julia> is_mutable(Bar), is_mutable(b)
(false,false)
检查不变性:
julia> is_immutable(Foo), is_immutable(f)
(false,false)
julia> is_immutable(Bar), is_immutable(b)
(true,true)
为了性能,还可以考虑将这些函数声明为 @pure
:
Base.@pure is_mutable(x::DataType) = x.mutable
从 julia 1.7 开始,有一个新的标准库函数 ismutabletype
。如果你真的想要 ismutable
处理类型,你可以为它添加另一个方法,如下所示:
function ismutable(t::Type) ::Bool
return ismutabletype(t)
end
用法:
mutable struct MutableType end
struct ImmutableType end
mutable_instance = MutableType()
immutable_instance = ImmutableType()
ismutable(mutable_instance) # true
ismutable(typeof(mutable_instance)) # true
ismutable(immutable_instance) # false
ismutable(typeof(immutable_instance)) # false
我只推荐使用 Base.ismutabletype
开始,在大多数情况下不推荐向标准库函数添加方法