[swift-evolution] Binding generic types using a metatype parameter
Joe Groff
jgroff at apple.com
Thu Mar 24 21:54:39 CDT 2016
> On Mar 24, 2016, at 4:04 PM, Joanna Carter <joanna at carterconsulting.org.uk <mailto:joanna at carterconsulting.org.uk>> wrote:
>
> Unfortunately, this doesn't compile…
>
> extension PropertyType
> {
> private static func create() -> Property<Self>
> {
> return Property<Self>.init()
> ^ '>' is not a postfix unary operator
> }
> }
>
> Which is why I found it necessary to add a typealias like so…
>
> extension PropertyType
> {
> private static func create() -> Property<Self>
> {
> typealias SelfType = Self
>
> return Property<SelfType>.init()
> }
> }
Oops, that's definitely a bug.
> This doesn't compile either…
>
> struct PropertyFactory
> {
> static func createBoundPropertywithValueType(valueType: PropertyType.Type) -> PropertyProtocol
> {
> return valueType.create()
> ^ Member 'create' cannot be used on value of protocol type 'PropertyType'; use a generic constraint instead
> }
> }
>
> For some reason, as soon as you change the PropertyType extension static func to return PropertyProtocol…
>
> extension PropertyType
> {
> private static func create() -> PropertyProtocol
> {
> typealias SelfType = Self
>
> return Property<SelfType>.init()
> }
> }
>
> … then, and only then, the factory's method compiles correctly.
Oops, my fault, this one is by design. Since we're calling through a dynamic type, our type system isn't able to express the result type `Property<valueType>`, so we need to abstract it behind the PropertyProtocol.
> Of course, after all this effort, I still have the problem of how to get the Any.Type from the SubjectType property of a Mirror into a PropertyType.Type.
That one's easy, at least—you can take `Any.Type` and use `as?` to cast it to `PropertyType.Type`.
> What would really make life easier here would be the ability to extend Any.
>
> Any chance?
I think that'd be cool. Some people fear the power (and ensuing responsibility) that unleashes, though…
-Joe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160324/1292015d/attachment.html>
More information about the swift-evolution
mailing list