Re: protocol extension with SequenceType
Re: protocol extension with SequenceType
- Subject: Re: protocol extension with SequenceType
- From: Quincey Morris <email@hidden>
- Date: Thu, 09 Jul 2015 18:31:31 +0000
On Jul 9, 2015, at 03:20 , Roland King <email@hidden> wrote:
>
> What I’m trying to do here is extend Array to conform to a protocol. The real example has constraints on the Element of the Array, so I can’t just extend Array, I have to use a protocol extension on the protocol (I can’t find a way to extend Array to conform to a type and also have some constraints).
I’ve been on this same merry-go-round for 3 days, and I have found no way of doing this. Until yesterday, extending an Array with conformance to a protocol AND with a constraint simply crashed the compiler. As of beta 3, it no longer crashes, but gives the same message that SequenceType gives: you can’t have conformance and a constraint on the same extension.
> Since an Array is a SequenceType and that’s all I really need, I tried this, this is the very distilled version
>
> protocol Foo
> {
> }
>
> extension Foo where Self : SequenceType
> {
> func bar()->Self
> {
> let retval : Array<Self.Generator.Element> = []
> return retval // error: cannot convert return expression of type 'Array<Self.Generator.Element>' to expected return type 'Self'
> }
> }
I had no luck with this kind of approach either, although what I tried was more like this:
extension Array where Element: Foo { … }
but I got nowhere with this either.
> but I get the given error message and don’t see why (nor how to fix it). An Array is a SequenceType, so one should be returnable as a ‘Self’ as long as it’s an array of the right elements. Self.Generator.Element should be the correct element type for the array. Compiler bug or something I’m missing?
I don’t think it’s a bug, in the sense that something that’s supposed to work doesn’t. Instead, I think that there are multiple meanings of “Self” that don’t fit into a single landscape. I’ve certainly had error messages saying "cannot convert return expression of type ’Self' to expected return type ‘Self’”.
I also don’t believe I’ve ever found a situation in a (static or instance) method with return type -> Self where I could find *any* value to return that didn’t produce an error. That is, I couldn’t find *any* way of using Self like Obj-C’s instancetype. However, I haven’t tried to do this in beta 3 yet, so maybe something has changed in this area.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden