<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Swift Generics Demystified on Daniel Lyons</title><link>https://dandylyons.net/posts/series/swift-generics-demystified/</link><description>Recent content in Swift Generics Demystified on Daniel Lyons</description><generator>Hugo -- gohugo.io</generator><language>en-gb</language><lastBuildDate>Mon, 01 Apr 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://dandylyons.net/posts/series/swift-generics-demystified/index.xml" rel="self" type="application/rss+xml"/><item><title>Concrete and "Soft" Types in Swift</title><link>https://dandylyons.net/posts/swift-generics/concrete-types-swift/</link><pubDate>Mon, 01 Apr 2024 00:00:00 +0000</pubDate><guid>https://dandylyons.net/posts/swift-generics/concrete-types-swift/</guid><description>&lt;p>In our &lt;a href="https://dandylyons.net/posts/swift-generics/explicit-types-swift/">last article&lt;/a>, we learned about how the generics system is deeply integrated into Swift at practically every level. This can give us magical features that help like &lt;em>Type Inference&lt;/em> which makes our code easier to read and right, but it can also lead frustrating and confusing compile-time errors. Furthermore, most modern Swift libraries are filled with generic code, especially in Apple first-party frameworks such as &lt;strong>SwiftUI&lt;/strong>, &lt;strong>Combine&lt;/strong>, and the recently announced &lt;strong>SwiftData&lt;/strong>. I hope that I&amp;rsquo;ve made a strong case that &lt;strong>generics in Swift are simply too important to ignore&lt;/strong>. So without further ado, let&amp;rsquo;s dive into generics, albeit with a slightly different approach than you might expect.&lt;/p></description></item><item><title>Explicit and Implicit Types in Swift</title><link>https://dandylyons.net/posts/swift-generics/explicit-types-swift/</link><pubDate>Sat, 16 Mar 2024 00:00:00 +0000</pubDate><guid>https://dandylyons.net/posts/swift-generics/explicit-types-swift/</guid><description>&lt;p>Generics are one of the most powerful features in Swift, yet they can often feel overwhelming, even for seasoned Swift developers. In this series we&amp;rsquo;ll learn how to make generics simple, useful, and even fun!&lt;/p>
&lt;h3 id="back-to-basics">Back to Basics&lt;/h3>
&lt;p>But to start off, we&amp;rsquo;ll look somewhere you probably won&amp;rsquo;t expect: declaring variables.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-swift" data-lang="swift">&lt;span class="line">&lt;span class="cl">&lt;span class="kd">let&lt;/span> &lt;span class="nv">strings&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="p">[&lt;/span>&lt;span class="s">&amp;#34;John&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s">&amp;#34;Paul&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s">&amp;#34;George&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s">&amp;#34;Ringo&amp;#34;&lt;/span>&lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="kd">let&lt;/span> &lt;span class="nv">oneLongString&lt;/span> &lt;span class="p">=&lt;/span> &lt;span class="n">strings&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="n">joined&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="n">separator&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s">&amp;#34;, &amp;#34;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This seemingly simple piece of code has some hidden functionality. Consider for a second, what type is &lt;code>strings&lt;/code>. That&amp;rsquo;s easy. It&amp;rsquo;s an &lt;code>Array&lt;/code>. But that answer is only half correct. Notice, how does &lt;code>strings&lt;/code> know about the &lt;code>joined&lt;/code> method? How does it know how to join the elements? What if that was an array of numbers?. Here, most Swift developers would say that the answer is &lt;a href="https://docs.swift.org/swift-book/documentation/the-swift-programming-language/types/#Type-Inference">Type Inference&lt;/a>. And while that answer is technically correct, it&amp;rsquo;s still missing part of the story.&lt;/p></description></item></channel></rss>