You must define the protocol as a class protocol:
protocol ValueProvider : class { var value: String {get set} }
Then
var value: String { get { return v.value } set { v.value = newValue } }
compiles and works as expected (i.e. assigns a new value to the object referenced by v1 if v1 != nil , and to the object referenced by v2 otherwise).
v is a read- ValueProvider property of type ValueProvider . By defining a protocol as a class protocol, the compiler knows that v is a reference type, and therefore its v.value property can be changed, even if the link itself is a constant.
Your original code example works because the v property is of type A , which is a reference type.
And your workaround
set { var tmp = v1 ?? v2 tmp.value = newValue }
works because the properties of variables (read-write) can be set in anyway (value type or reference type).
source share