3 ms·
To further my understanding this would be akin to trait objects in Rust right? Instead of having generic type constraints in the function signature you can `Box
by squiguy7 9y ago
To further my understanding this would be akin to trait objects in Rust right? Instead of having generic type constraints in the function signature you can `Box` your traits or use pointers to a trait and use dynamic dispatch. I'm sure there are also some differences as well and would like to hear them.
- ridiculous_fish 9y agoThe direct analog of Rust's trait objects is Swift's protocol objects; call them existential types. Both Rust and Swift have limitations on which traits (protocols) can be used as an existential. Swift forbids existentials when the protocol has an associated type. Rust has a notion of "object safety" which is both more flexible and more complicated, but applies the same restriction in most cases. I think the closest analog of Swift's `AnySequence<Int>` would be a `Box<Iterator<Item=Int>>`.
- Veedrac 9y agoIf you're interested, there's a very nice talk on how Swift's generics compile. It's different to Rust in some key ways. https://www.youtube.com/watch?v=ctS8FzqcRug https://www.youtube.com/watch?v=ctS8FzqcRug
- mikeash 9y agoThanks for posting this, I've been meaning to watch it but keep forgetting about it.