swift - Cannot assign to property: function call returns immutable value -
consider following example.
struct astruct{ var = 0 } class aclass{ var = 0 var a: = a(i: 8) func astruct() -> astruct{ return } }
if try mutate the variable of instance of class aclass
compiles successfully.
var ca = aclass() ca.a.i = 7
but if try mutate return value of astruct
method, compile screams
ca.astruct().i = 8 //compile error. cannot assign property: function call returns immutable value.
can explain this.
this compiler's way of telling modification of struct
useless.
here happens: when call astruct()
, copy of a
passed you. copy temporary. can examine fields, or assign variable (in case able access modifications back). if compiler let make modifications temporary structure, have no way of accessing them back. why compiler programming error.
Comments
Post a Comment