jrose
(Jordan Rose)
November 9, 2018, 4:35pm
2
It's come up before:
I have a model like this:
protocol Promise {
associatedtype Result
}
protocol Scanner {
associatedtype ScanPromise: Promise
func promiseScan<T>(from: Offset, until: (Offset, Item) -> T?) -> ScanPromise // where Result == T?
}
The thing that I’m trying to express is: whichever type implements the associated type ‘ScanPromise’ must be generic, and that parameter must be its result (i.e. something it got as a result of calling the “until” closure).
Even with SE-0142, this kind …
There are certainly use cases for it, but it's not easy to implement. I I think it counts as a limited form of "higher-kinded types", which have been discussed a few times as well (though in a slightly different form).
Hello,
A potent problem in my experience with Swift has been the lack of a way to generalize code that is repetitive on the basis of higher kinded types. For the uninitiated, higher kinded types are the ability to reason about generic types with their type parameters as variable. For example, the Haskell definition for Functor, and a corresponding definition for Maybe (Haskell's equivalent to Optional)
typeclass Functor f where
fmap :: (a -> b) -> f a -> f b
instance Functor …