<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jérémie Girault on Medium]]></title>
        <description><![CDATA[Stories by Jérémie Girault on Medium]]></description>
        <link>https://medium.com/@kamidude?source=rss-ac82b2d2e850------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wHARgK019QocNqI5N8PQyA.jpeg</url>
            <title>Stories by Jérémie Girault on Medium</title>
            <link>https://medium.com/@kamidude?source=rss-ac82b2d2e850------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 23:04:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kamidude/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The why of nonmutating]]></title>
            <link>https://medium.com/swift-programming/the-why-of-nonmutating-7ecd2cf17ecf?source=rss-ac82b2d2e850------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ecd2cf17ecf</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Jérémie Girault]]></dc:creator>
            <pubDate>Mon, 02 May 2016 10:22:49 GMT</pubDate>
            <atom:updated>2016-05-02T13:36:32.175Z</atom:updated>
            <content:encoded><![CDATA[<p>I saw a few tweets and questionings recently about the nonmutating keyword in swift. I had to confront it sometime in the past so I will try to expose a justification of it’s existence with a simple example.</p><h4>Scenario</h4><p>Let’s assume that we want to create a protocol for a settings storage such as this one :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/076493bc3046eb3084e9f02eb8fb3168/href">https://medium.com/media/076493bc3046eb3084e9f02eb8fb3168/href</a></iframe><p>Simple enough ! Let’s implement it with NSUserDefaults :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6113fcefa3311ee30104ad67ba91d93e/href">https://medium.com/media/6113fcefa3311ee30104ad67ba91d93e/href</a></iframe><p>Right! now let’s try to use it :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afea070c490dcd883411bb1a0f5a9f93/href">https://medium.com/media/afea070c490dcd883411bb1a0f5a9f93/href</a></iframe><p>Great!</p><h4>Rationale</h4><p>However using the setter does not work as expected :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*I018UZOccm9cb2oj1x2r8w.png" /></figure><p>Why ? Because the protocol could be implemented by any value or reference type.</p><ul><li>In the case of a reference type such as NSUserDefaults the compiler don’t even care because there is no constness compiler check such as in value types.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0dd46ea6d1ac4c961c648acdf0a5573c/href">https://medium.com/media/0dd46ea6d1ac4c961c648acdf0a5573c/href</a></iframe><ul><li>In the case of a constant value type (declared using the let keyword), the compiler will prevent you statically to use any mutating accessor from either outside and inside your type.</li></ul><h4>Solution</h4><p>We have many solutions in this case :</p><ul><li>We could simply make it var as the fixit proposes. I really don’t like this version because immutability is generally better.</li><li>We could constrain our protocol to reference types and it would work, but we would not be allowed to implement it from value types. It’s a shame because we could create a non-mutating functional implementation such as this one :</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e51d96de9433a745ca725957557fe616/href">https://medium.com/media/e51d96de9433a745ca725957557fe616/href</a></iframe><p>Here my struct is not modified however we got this error</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h9omy3Wb0wTpyO-2h_l5TA.png" /></figure><h4>A better solution</h4><p>That is an example of how the nonmutating keyword is useful: you can explicitly tell the compiler that this accessor won’t mutate its enclosing type. For your own safety the compiler won’t let you access any mutating member of the type from this accessor and constancy will be preserved. But during compile-time checks, the compiler won’t prevent you to call the setter on a const instance of the given type !</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6aa9b093728cdc809b882a21cdd011e5/href">https://medium.com/media/6aa9b093728cdc809b882a21cdd011e5/href</a></iframe><p>However you will still be able to create some nice APIs using value types in these specific cases while keeping all of swift safety mechanisms !</p><p>This is in my opinion the most elegant solution swift provides us in this case.</p><p>What do you think ?</p><p><a href="https://twitter.com/kamidude">Follow me on twitter </a><br><a href="https://gist.github.com/jeremiegirault/d4710116e19ec86974550960ba52554a">Full code</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ecd2cf17ecf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-programming/the-why-of-nonmutating-7ecd2cf17ecf">The why of nonmutating</a> was originally published in <a href="https://medium.com/swift-programming">Swift Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generic type extension by retrofitting protocols in Swift 2]]></title>
            <link>https://medium.com/swift-programming/generic-type-extension-by-retrofitting-protocols-in-swift-2-79fb0e0e2ebe?source=rss-ac82b2d2e850------2</link>
            <guid isPermaLink="false">https://medium.com/p/79fb0e0e2ebe</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Jérémie Girault]]></dc:creator>
            <pubDate>Thu, 17 Dec 2015 00:21:19 GMT</pubDate>
            <atom:updated>2015-12-21T09:29:12.659Z</atom:updated>
            <content:encoded><![CDATA[<h4>Rationale</h4><p>A common Swift problem is that extensions of generics can only be constrained to protocols. We use a lot of big words here but this issue is quite common and a simple example will clearly illustrate this situation.</p><p>So, let’s assume that we want to detect if an optional string is empty or nil.</p><p>The basic solution would look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d72c3b6c5749f8c79f9cdf2f9653f50/href">https://medium.com/media/9d72c3b6c5749f8c79f9cdf2f9653f50/href</a></iframe><p>This snippet looks a bit bloated for such a simple operation and we would rather use something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99991f8509be99d8f0e6fffef30e90bd/href">https://medium.com/media/99991f8509be99d8f0e6fffef30e90bd/href</a></iframe><p>However, the <em>isEmpty</em> property does not exist in Optional type and we immediately think of adding an extension to Optional type to help with this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26c737910016a0d14cfe8b6196bbd20f/href">https://medium.com/media/26c737910016a0d14cfe8b6196bbd20f/href</a></iframe><p>But there we fall short on a Swift limitation :</p><blockquote>Type ‘Wrapped’ constrained to non-protocol type ‘String’.</blockquote><p>Unfortunately, swift only accepts protocol as constraints on generic type extensions :-(<br>Constraining generic protocols, especially, do not work on value types such as: structs, enums and base types.</p><p>An interesting thing is, that swift implicitly provides a protocol for classes describing their interface. You can therefore constraint to classes easily.</p><p>Now, lets see how to make constraints on value types.</p><h4>Solution</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d544c4099a37e10f0129178143a3f223/href">https://medium.com/media/d544c4099a37e10f0129178143a3f223/href</a></iframe><p>Let’s retrofit it on String: we only need to declare it, because the implementation already exists :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f89e2d100e17aa236e853d0aa1368b1c/href">https://medium.com/media/f89e2d100e17aa236e853d0aa1368b1c/href</a></iframe><p>Now let’s update our Optional extension to support the protocol we introduced:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3773a3ffca878ef4b242b0041c9a54ec/href">https://medium.com/media/3773a3ffca878ef4b242b0041c9a54ec/href</a></iframe><p>and, thats it!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ef60291e426918521017d184fb2ece7/href">https://medium.com/media/8ef60291e426918521017d184fb2ece7/href</a></iframe><p>Bonus code: we can even retrofit more implementations as long as they conform to this signature !</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa3b855e20cc6147d3b21d5d2cd15efa/href">https://medium.com/media/fa3b855e20cc6147d3b21d5d2cd15efa/href</a></iframe><p>Thank you for reading, I hope you enjoyed this post.</p><p>If you have any feedback, improvement or just want to say hello, here is my twitter <a href="https://twitter.com/kamidude">@kamidude</a></p><h3>Addendum</h3><p>I’ve received an interesting feedback that I would like to include in this post:</p><ul><li>Extending Optional (especially in libraries) may not be a good idea. Polluting base types and global namespace is not the best practice. However, this article intends to be an illustration of retrofitting / extending types with constraints.</li></ul><h4>Full code gist</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad13abd94294626c0e8eaeaa7474e329/href">https://medium.com/media/ad13abd94294626c0e8eaeaa7474e329/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=79fb0e0e2ebe" width="1" height="1" alt=""><hr><p><a href="https://medium.com/swift-programming/generic-type-extension-by-retrofitting-protocols-in-swift-2-79fb0e0e2ebe">Generic type extension by retrofitting protocols in Swift 2</a> was originally published in <a href="https://medium.com/swift-programming">Swift Programming</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generic type extension by retrofitting protocols in Swift 2]]></title>
            <link>https://medium.com/snips-ai/generic-type-extension-by-retrofitting-protocols-in-swift-2-c8fe194d757c?source=rss-ac82b2d2e850------2</link>
            <guid isPermaLink="false">https://medium.com/p/c8fe194d757c</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Jérémie Girault]]></dc:creator>
            <pubDate>Tue, 15 Dec 2015 09:43:57 GMT</pubDate>
            <atom:updated>2015-12-17T14:48:59.863Z</atom:updated>
            <content:encoded><![CDATA[<h4>Rationale</h4><p>A common Swift problem is that extensions of generics can only be constrained to protocols. We use a lot of big words here but this issue is quite common and a simple example will clearly illustrate this situation.</p><p>So, let’s assume that we want to detect if an optional string is empty or nil.</p><p>The basic solution would look something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d72c3b6c5749f8c79f9cdf2f9653f50/href">https://medium.com/media/9d72c3b6c5749f8c79f9cdf2f9653f50/href</a></iframe><p>This snippet looks a bit bloated for such a simple operation and we would rather use something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99991f8509be99d8f0e6fffef30e90bd/href">https://medium.com/media/99991f8509be99d8f0e6fffef30e90bd/href</a></iframe><p>However, the <em>isEmpty</em> property does not exist in Optional type and we immediately think of adding an extension to Optional type to help with this :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/26c737910016a0d14cfe8b6196bbd20f/href">https://medium.com/media/26c737910016a0d14cfe8b6196bbd20f/href</a></iframe><p>But there we fall short on a Swift limitation :</p><blockquote>Type ‘Wrapped’ constrained to non-protocol type ‘String’.</blockquote><p>Unfortunately, swift only accepts protocol as constraints on generic type extensions :-(<br>Constraining generic protocols, especially, do not work on value types such as: structs, enums and base types.</p><p>An interesting thing is, that swift implicitly provides a protocol for classes describing their interface. You can therefore constraint to classes easily.</p><p>Now, lets see how to make constraints on value types.</p><h4>Solution</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d544c4099a37e10f0129178143a3f223/href">https://medium.com/media/d544c4099a37e10f0129178143a3f223/href</a></iframe><p>Let’s retrofit it on String: we only need to declare it, because the implementation already exists :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f89e2d100e17aa236e853d0aa1368b1c/href">https://medium.com/media/f89e2d100e17aa236e853d0aa1368b1c/href</a></iframe><p>Now let’s update our Optional extension to support the protocol we introduced:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3773a3ffca878ef4b242b0041c9a54ec/href">https://medium.com/media/3773a3ffca878ef4b242b0041c9a54ec/href</a></iframe><p>and, thats it!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ef60291e426918521017d184fb2ece7/href">https://medium.com/media/8ef60291e426918521017d184fb2ece7/href</a></iframe><p>Bonus code: we can even retrofit more implementations as long as they conform to this signature !</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa3b855e20cc6147d3b21d5d2cd15efa/href">https://medium.com/media/fa3b855e20cc6147d3b21d5d2cd15efa/href</a></iframe><p>Thank you for reading, I hope you enjoyed this post.</p><p>If you have any feedback, improvement or just want to say hello, here is my twitter <a href="https://twitter.com/kamidude">@kamidude</a></p><h3>Addendum</h3><p>I’ve received an interesting feedback that I would like to include in this post:</p><ul><li>Extending Optional (especially in libraries) may not be a good idea. Polluting base types and global namespace is not the best practice. However, this article intends to be an illustration of retrofitting / extending types with constraints.</li></ul><h4>Full code gist</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad13abd94294626c0e8eaeaa7474e329/href">https://medium.com/media/ad13abd94294626c0e8eaeaa7474e329/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c8fe194d757c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/snips-ai/generic-type-extension-by-retrofitting-protocols-in-swift-2-c8fe194d757c">Generic type extension by retrofitting protocols in Swift 2</a> was originally published in <a href="https://medium.com/snips-ai">Snips Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>