當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。