当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Julia Core.setfield!用法及代码示例


用法:

setfield!(value, name::Symbol, x, [order::Symbol])
setfield!(value, i::Int, x, [order::Symbol])

x 分配给复合类型value 中的命名字段。 value 必须是可变的,并且 x 必须是 fieldtype(typeof(value), name) 的子类型。此外,可以为此操作指定排序。如果该字段声明为 @atomic ,则此规范是强制性的。否则,如果未声明为 @atomic ,则必须为 :not_atomic(如果指定)。另见 setproperty!

例子

julia> mutable struct MyMutableStruct
           field::Int
       end

julia> a = MyMutableStruct(1);

julia> setfield!(a, :field, 2);

julia> getfield(a, :field)
2

julia> a = 1//2
1//2

julia> setfield!(a, :num, 3);
ERROR: setfield!: immutable struct of type Rational cannot be changed

相关用法


注:本文由纯净天空筛选整理自julialang.org 大神的英文原创作品 Core.setfield! — Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。