protocol ValueHolder {
}
struct A: ValueHolder {
var value = 5
}
var x: ValueHolder = A()
func f(a: inout A) {
a.value = 10
}
I want to use pass x to f. Is it possible?
Edit: I understand all of the staff about value semantics and inout parameters. The problem is that x
needs to be casted to A
and I wonder if that can be done without copying it (as
makes a typed copy of it) so that I can use f
on x
.
Not exactly — at least, not if the parameter is an A. An A is a struct. Even with
inout
, a struct is still a value type.inout
allows the original value to be replaced, but what it is replaced with is another A. Simply put, a struct cannot be mutated in place.That being so, your question seems to lose its meaning. Since a struct cannot be mutated in place, there is no real reason to use
inout
here at all. You are not doing anything that calls forinout
. You might as well just drop theinout
and accept the value semantics:If you really want to keep the
inout
, then typea:
as a ValueHolder and cast inside the function, like this: