Setting value to named field in Julia – setfield() Method
Last Updated :
24 Feb, 2021
The setfield() is an inbuilt function in julia which is used to assign a value x to a named field in value of composite type. The value must be mutable and x must be a subtype of fieldtype(typeof(value), name)
Syntax:
setfield(value, name::Symbol, x)
Parameters:
- value: Specified value of composite type.
- name::Symbol: Specified symbol.
- x: Specified value.
Returns: It returns the assigned value x to a named field in value of composite type.
Example 1:
Python
mutable struct MyMutableStruct
field:: Int
end
a = MyMutableStruct( 1 );
setfield (a, :field, 123 );
println(getfield(a, :field))
|
Output:
123
Example 2:
Python
a = 5 / / 3
println(setfield (a, :num, 123 ))
|
Output:
ERROR: LoadError: type Rational is immutable
while loading /home/cg/root/5090533/main.jl, in expression starting on line 7
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...