Not quite sure how to phrase a good title for that ...
I'd like to define a generic protocol GBucket
which is kinda like a set of items of the same type. A bit like a CollectionType
, but with far less capabilities. Like so:
public protocol GBucket {
associatedtype BElement
func splitBucket
<A: GBucket, B: GBucket where A.BElement == Self.BElement,
B.BElement == Self.BElement>
(idx: Int) -> ( A, B )
}
It essentially just provides a method to split a GBucket
into two new GBucket
s. Which can be of any type conforming to the protocol - i.e. the returned parts do not have to be the same class doing the split.
I tried that as a sample implementation:
extension ArraySlice : GBucket {
public typealias BElement = Generator.Element
public func splitBucket
<A: GBucket, B: GBucket where A.BElement == BElement,
B.BElement == BElement>
(idx: Int) -> ( A, B )
{
let ls : ArraySlice<A.BElement> = self[0..<idx]
let a : A = ls // this conversion FAILs
return ( a, self[idx..<self.count] ) // this too, of course ;-)
}
}
This produces:
Cannot convert value of type 'ArraySlice < Element >' to specified type 'A'
Which as far as I can tell should convert just fine. ArraySlice
is a GBucket
and the element type is the same thanks to the where
specification.
And another, shorter sample illustrating the issue which doesn't use Array stuff:
public protocol GBucket {
associatedtype BElement
func otherBucket<A: GBucket where A.BElement == Self.BElement>() -> A
}
public class MyBucketType<T> : GBucket {
public typealias BElement = T
public func otherBucket<A: GBucket where A.BElement == BElement>() -> A {
return MyBucketType<A.BElement>()
}
}
What me doin' wrong?