It would be great if we could write generic constraints which are expressed in terms of other generic constraints. It would allow us to create much more powerful constructs with greater expressiveness to the type-system.
One example which would be helpful:
enum Either<T1,T2>
{
case A( T1 )
case B( T2 )
func asCommonType<T where T1:T, T2:T>( type: T.Type ) -> T
{
switch self
{
case .A( let val ): return val as! T
case .B( let val ): return val as! T
}
}
}Then I could use the Either type like this:
let someView : Either<UIButton, UIImageView>
...
view.addSubview( someView.asCommonType( UIView.Type ) )With compile-time guarantees which restrict the types that someView may be assigned, and also when it is used that it may be represented as a UIView. If anybody changes the code in a way which violates this relationship, it won't compile.
Currently this isn't possible, as the compiler complains on line 06 that "T" isn't a protocol type. I can't represent this type relationship statically to the Swift compiler.