[swift-evolution] [Pitch] Extend Any.Type to allow construction of bound generic types

Douglas Gregor dgregor at apple.com
Mon Apr 18 16:07:40 CDT 2016


> On Apr 17, 2016, at 7:13 AM, Joanna Carter via swift-evolution <swift-evolution at swift.org> wrote:
> 
> I often find myself needing to construct an instance of a bound generic type at runtime, in much the same manner as I would a "standard" type.
> 
> e.g.
> 
> As for a standard type…
> 
> let aType = Int.Type
> 
> let anInt = aType.init(0)
> 
> I also want to be able to use the same mechanism for a generic type…
> 
> struct MyStruct<T: Equatable /*, etc */>
> {
>  var value: T?
> }
> 
> let paramType = // type from streaming or reflection compliant with Equatable
> 
> let structType = MyStruct<paramType>.self
> 
> let f = structType.init()
> 
> 
> At present, the following compiler error is raised…
> 
> 'paramType' is not a type
> 
> I am  the only one to require this behaviour? If not, do we want a proposal? If so, could you let me know your views on its worthiness?


In programming-language circles, this feature is called “dependent types” (see, e.g., https://en.wikipedia.org/wiki/Dependent_type), and it introduces significant complicates into a type system. For example, determining whether two types are equivalent becomes a run-time property rather than a compile-time property. I don’t know if Swift will end with a dependently-typed type system. To get there, we would need a number of very strongly-motivating use cases illustrating how common programming tasks can be improved with dependent types,  and we would need to solid plan for managing the complexity—both implementation complexity in the compiler’s type checker and also the language complexity seen by Swift user’s when they encounter this feature. 

	- Doug



More information about the swift-evolution mailing list