Using a delegate pattern, I'd like to write the following:
protocol VariableDelegate {
typealias Object
func canAssign (variable: Variable<Object>, value:Object) -> Bool
}
class Variable<Object> {
var value : Object
var delegate : VariableDelegate<Object>?
func assign (value: Object) {
if (delegate?.canAssign(self, value: value) ?? true) {
self.value = value
}
}
}But alas the `var delegate` declaration is flagged for 'associated type requirements'. What is a preferred approach to such a pattern?
I know I can write this as:
protocol VariableDelegate {
typealias Object
func canAssign (variable: Variable<Object,Self>, value:Object) -> Bool
}
class Variable<Object, Delegate:VariableDelegate where Delegate.Object == Object> {
var value : Object
var delegate : Delegate?
// ...
}but then I need to propagate the second type parameter as Variable<Object, SomeDelegate<Object>> which I am loath to do. I'm obviously not thinking about protocols and types properly. Can you help me with a mindset shift?