Mutating function in Julia (function that modifies its arguments)

后端 未结 3 1441
孤城傲影
孤城傲影 2021-02-05 13:54

How do you define a mutating function in Julia, where you want the result to be written to one of it\'s inputs.

I know functions exist like push!(list, a),

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-05 14:32

    For a struct type, you can use setfield!(value, name::Symbol, x) and getfield(value, name::Symbol) inside a function.

    When you call the function, you need to pass the struct obj/name (value) and the field symbol (name). (x) is the new value for the struct field.

提交回复
热议问题