Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rnapier/myanysequence
https://github.com/rnapier/myanysequence
Last synced: 2 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/rnapier/myanysequence
- Owner: rnapier
- Created: 2015-10-06T23:01:25.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2015-10-28T15:12:55.000Z (about 9 years ago)
- Last Synced: 2023-03-22T19:07:23.724Z (over 1 year ago)
- Language: Swift
- Size: 94.7 KB
- Stars: 18
- Watchers: 3
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
When [last we talked about type erasure](http://robnapier.net/erasure), I described an easy way to build type erasures using closures. And I mentioned:
> (While this works exactly like AnySequence, this isn’t how AnySequence is implemented. In my next post I’ll discuss why and how to implement type erasers like stdlib does.)
At the time I thought I'd pretty well nailed it down, but every time I dug into it I found another little thing I'd missed, and it never seemed to end. And with stdlib open sourcing soon, you'll all just be able to read this yourselves, so why embarrass myself getting it all wrong? Over time I kind of hoped you all had forgotten that comment and planned to move on to other things. But then I was [busted by Michael Welch](https://twitter.com/My_kl/status/650796108789219328), and so I had to finish the spelunking and here you go.
So, here is my annotated implementation of `AnyGenerator` and `AnySequence` that I believe pretty closely matches Apple's implementation in stdlib (minus some low-level optimizations in `AnySequence` that I'll call out when we get there). I've named my versions of public symbols with a trailing underscore to differentiate from the Swift version. For private symbols (with leading underscore), I've used the same name Swift does.
All type information was determined using `:type lookup` in the Swift REPL. In a few places I checked the implementation with Hopper, but in most cases, once you have the types, the implementation is obvious. (There's a deep lesson in there if you pay attention.)