In Swift I have a protocol that represents an object that wraps a value. I'd like to add a mapping function to that protocol that indicates that some mapping function will be applied to that value to return another instance of that protocol. My protocol looks like this so far:
protocol Foo {
typealias Value
func flatMap<T>(map: (Value) -> T) -> Self
...
Basically I want flatMap to take a block which is passed in Value and returns T and I want the "Self" that's returned to be specialized on T. This isn't quite right though because Self is the type of the protocol. Really what I want to return is "Self" but I can't do that obviously.