[Pre-proposal/Idea] Auto-generate Type-Erased Wrappers


(Haravikk) #1

One of the things that threw me when starting with Swift was the inability to use many protocols as types, e.g- being unable to declare: [SequenceType], instead having to do: [AnySequence]

And I guess it got me wondering; why can’t we just have these be auto-generated and used where we would expect a protocol to fit? Writing type-erased wrappers currently involves a lot of boilerplate, especially for a type with a lot of methods and/or properties, so automating this would be very useful.

For me there are two good options:

Autogenerate for All Types: As it says; every protocol would have an identically named type-erasure (with the same visibility etc.) that Swift would just swap in where appropriate. For example:

  protocol Foo { … }
  let myArray:[Foo] // Array of type-erased wrappers of Foo

Autogenerate by Attribute: Same basic idea, except that an attribute would be required to specify which protocols get automatic type-erasure like so:

  @autoErasure protocol Foo { … }
  let myArray:[Foo] // Array of type-erased wrappers of Foo

I’m curious for people’s thoughts on the matter. I know that I for one am tired of defining type-erased wrappers that are just a heap of boiler-plate to properly wrap an instance. It’s also something I’ve seen people doing wrong (and done wrong myself), for example by using closures to invoke methods on an instance; this is a much more elegant looking solution but very quickly bloats the size of the type-erased wrapper compared to more correct boxing method (though for single method protocols it may be fine). It seems like something that would be good to take out of the hands of developers who mostly don’t want to have to define these in the first place, though of course the capability to defining customised versions would remain if anyone actually needs it.

I’m particularly curious about other features in discussion or that are upcoming that may affect this, as I’m sure I’ll have missed some. Anything that might render this obsolete (or less necessary)? Also whether anyone thinks it could be a bad idea to essentially hide the type-erasure.


(Russ Bishop) #2

One of the things that threw me when starting with Swift was the inability to use many protocols as types, e.g- being unable to declare: [SequenceType], instead having to do: [AnySequence]

And I guess it got me wondering; why can’t we just have these be auto-generated and used where we would expect a protocol to fit? Writing type-erased wrappers currently involves a lot of boilerplate, especially for a type with a lot of methods and/or properties, so automating this would be very useful.

For me there are two good options:

Autogenerate for All Types: As it says; every protocol would have an identically named type-erasure (with the same visibility etc.) that Swift would just swap in where appropriate. For example:

  protocol Foo { … }
  let myArray:[Foo] // Array of type-erased wrappers of Foo

The only reason you can’t use [Foo] is due to associated type or Self requirements so what type is Foo’s associated type(s) here?

I’m particularly curious about other features in discussion or that are upcoming that may affect this, as I’m sure I’ll have missed some. Anything that might render this obsolete (or less necessary)? Also whether anyone thinks it could be a bad idea to essentially hide the type-erasure.

The Completing Generics manifesto discusses this in the context of Existential types. If that were supported, then the compiler would just treat the associated types as Any. It also discusses allowing you to create types ala protocol<SequenceType where Element == String>

Russ

···

On Apr 4, 2016, at 3:07 PM, Haravikk via swift-evolution <swift-evolution@swift.org> wrote: