Swift Generalized Existentials
Are there any proposals or discussions currently present on introducing Generalized Existentials into Swift, what are the plans? If no, how can I participate and affect this?
It is a commonly requested feature, and there was already preliminary design effort on swift-evolution. But at this moment, the core team and the community are focusing on ABI stability affecting features, or what Lattner defines as "Swift 4 Phase 1".
You would definitely hear more about it when Phase 2 commences. Given its popularity, it is expected to be part of Swift 4.
If Swift was designed that way (with Associated Types, but without Generalized Existentials), maybe it implies some architectural shift. What am I expected to replace delegation pattern with?
You can use type-erased wrappers as a transitive solution. In general, it exploits the dynamic dispatch and the inheritance of classes to erase the type.
protocol Fancy {
associatedtype Value
var value: Value
}
struct FancyMatter<Value> {
let value: Value
}
class AnyFancyBoxBase<P: FancyProtocol>: AnyFancyBox<P.Value> {
let base: P
override var value: P.Value { return base.value }
init(_ base: P) { self.base = base }
}
class AnyFancyBox<Value> {
var value: Value { fatalError() }
}
var box: AnyFancyBox<Int> = AnyFancyBoxBase(FancyMatter(1))
You may take a look at how the Standard Library implements type-erased wrappers.
That code is not correct. The correct code, freshly compiled, is:
protocol Fancy {
associatedtype Value
var value: Value {get set}
}
struct FancyMatter<Value>: Fancy {
var value: Value
}
class AnyFancyBoxBase<P: Fancy>: AnyFancyBox<P.Value> {
let base: P
override var value: P.Value {return base.value }
init(_ base: P) { self.base = base }
}
class AnyFancyBox<Value> {
var value: Value {fatalError() }
}
var box: AnyFancyBox<Int> = AnyFancyBoxBase(FancyMatter(value: 1))
// prove it out
print (AnyFancyBoxBase(FancyMatter(value: 1)).value)
print (box.value)