mikeash.com pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html commentshttp://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html#commentsmikeash.com Recent CommentsFri, 29 Mar 2024 05:33:29 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssAlex - 2020-11-07 03:01:05http://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html#commentsYou mention Keyed{En,De}codingContainer. Does type erasure have anything to do with why (for example) JSONEncoder isn't-an Encoder?8097bfa3a0a6fd03fab00d9024c7fd97Sat, 07 Nov 2020 03:01:05 GMTSasi Binu - 2018-07-18 12:55:13http://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html#commentsclass IteratorImpl { ... <br /> <br />should be <br /> <br />private IteratorImpl { ... <br /> <br />I guess making "private" at: <br /> <br />private class MAnySequenceImpl ... { ... <br /> <br />try this one12f1f43429671b0f4245659b93bf6206Wed, 18 Jul 2018 12:55:13 GMTTim - 2018-07-12 09:15:21http://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html#commentsHi Mike, thanks for the article! <br /> <br />Do you know of any reason why Swift doesn't support generic protocols? Is there some issue at the core of the language ideology/implementation that would make implementing it impossible?3bfd9b56844deddd8c3bb7e6d8e58bcdThu, 12 Jul 2018 09:15:21 GMTYoungerman - 2017-12-23 05:14:15http://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html#commentsChange: <br /> <br /><code>init&lt;C: Collection&gt;(_ c: C) where C.Element == Element, C.Index == Int {</code> <br /> <br />To: <br /> <br /><code>init&lt;C: Collection&gt;(_ c: C) where C.Element == Element, C.Index == Int, C.IndexDistance == Int {</code> <br /> <br />Makes <br /> <br /><code>class GenericDataSource&lt;Element&gt; { ... }</code> <br /> <br />compile. Not sure if this is the right thing to do. But it compile now...7bf630e200dd009e9791ef2dbd096effSat, 23 Dec 2017 05:14:15 GMTYoungerman - 2017-12-21 16:49:08http://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html#comments<code>class GenericDataSource&lt;Element&gt; { ...</code> <br /> <br />doesn't compile: <br /> <br /><code>count = { c.count }</code> <br /> <br /><div class="blogcommentquote"><div class="blogcommentquoteinner">error: cannot convert value of type 'C.IndexDistance' to closure result type 'Int'</div></div>94e8015e1eace9cedaffa416640668c2Thu, 21 Dec 2017 16:49:08 GMTYoungerman - 2017-12-19 20:57:54http://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html#commentsAnd ... <br /> <br /><code>class IteratorImpl { ...</code> <br /> <br />should be <br /> <br /><code>private IteratorImpl { ...</code> <br /> <br />I guess making "private" at: <br /> <br /><code>private class MAnySequenceImpl ... { ...</code> <br /> <br />is not enough. Nested class inside private class is still accessible from outside?22530b298d8e2c884f35668f6b32df56Tue, 19 Dec 2017 20:57:54 GMTYoungerman - 2017-12-19 00:56:18http://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html#commentsIn the first example, Type Erasure with Class, should this: <br /> <br />override func makeIterator() -&gt; IteratorImpl { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return IteratorImpl(seq.makeIterator()) <br />&nbsp;&nbsp;&nbsp;&nbsp;} <br /> <br />be this?: <br /> <br />override func makeIterator() -&gt; Iterator { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return IteratorImpl(seq.makeIterator()) <br />&nbsp;&nbsp;&nbsp;&nbsp;} <br /> <br />IteratorImpl is implementation details that should not be exposed, right?41f5a2d676533ebcdb893e5dbc00b2f7Tue, 19 Dec 2017 00:56:18 GMTConstantino Tsarouhas - 2017-12-15 22:02:10http://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html#commentsI hope Swift gets generalised existentials some day so that you could write <code>Any&lt;Sequence where .Element == String&gt;</code> and get automatic erasure of <code>Self</code> and other associated types. <br /> <br />The stdlib can then define <code>typealias AnySequence&lt;T&gt; = Any&lt;Sequence where .Element == T&gt;</code> because that particular form of <code>Sequence</code> erasure is extremely common. And so on. <br /> <br />In my own projects, I heavily use protocols and associated types, but not having a built-in erasure feature makes it hard to do protocol-oriented programming and at the same time actually do real work instead of writing &amp; testing (error-prone) boilerplate.3de83ae40a347efd700dc2f7dcd43980Fri, 15 Dec 2017 22:02:10 GMTmikeash - 2017-12-15 17:28:46http://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html#comments<b>Jonah</b>: Thanks for pointing that out. I just took what Xcode gave me for autocompletion, and the protocol has <code>mutating</code> in it. It's harmless to have it here, but unnecessary, so I'll remove it.2c640c51704828ac988df3a6758165c4Fri, 15 Dec 2017 17:28:46 GMTJonah - 2017-12-15 17:21:09http://www.mikeash.com/?page=pyblog/friday-qa-2017-12-08-type-erasure-in-swift.html#commentsHi Mike, thanks for the article. Small point: In "Type Erasure With Functions," I don't think the Iterator's "next" method needs to be mutating because the reference to _next does not change; state is maintained in the closure.57ccf2f380202b453859393ebbfa01eaFri, 15 Dec 2017 17:21:09 GMT