<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Asaf Huseyn</title>
    <description>The latest articles on DEV Community by Asaf Huseyn (@asafhuseyn).</description>
    <link>https://dev.to/asafhuseyn</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1672115%2Ffd5b8577-5ae8-4764-a448-1c0739b3c1f3.jpeg</url>
      <title>DEV Community: Asaf Huseyn</title>
      <link>https://dev.to/asafhuseyn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/asafhuseyn"/>
    <language>en</language>
    <item>
      <title>Are All Swift Functions Actually Returning Tuples?</title>
      <dc:creator>Asaf Huseyn</dc:creator>
      <pubDate>Sat, 06 Jul 2024 19:25:51 +0000</pubDate>
      <link>https://dev.to/asafhuseyn/are-all-swift-functions-actually-returning-tuples-o</link>
      <guid>https://dev.to/asafhuseyn/are-all-swift-functions-actually-returning-tuples-o</guid>
      <description>&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;The Genesis of the Theory&lt;/li&gt;
&lt;li&gt;Unraveling the Tuple Mystery&lt;/li&gt;
&lt;li&gt;The Swift Type System: A Deeper Dive&lt;/li&gt;
&lt;li&gt;Void in Swift vs. Other Languages&lt;/li&gt;
&lt;li&gt;Implications and Reflections&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;li&gt;References&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Swift, known for its expressive and powerful type system, continues to surprise developers with its intricate design decisions. Today, we're diving deep into a fascinating theory that could reshape our understanding of Swift's function return types: Are all Swift functions secretly returning tuples? (Like SwiftUI does for Views)&lt;/p&gt;

&lt;h2&gt;
  
  
  The Genesis of the Theory
&lt;/h2&gt;

&lt;p&gt;This intriguing question arose while investigating the subtle differences between &lt;code&gt;()&lt;/code&gt; and &lt;code&gt;Void&lt;/code&gt; in Swift. At first glance, these two seem interchangeable, often described as representing "nothing" or an empty return type. However, a closer look reveals some surprising behavior:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This works fine&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;emptyTuple&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// This causes an error: "Expected member name or constructor call after type name"&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;emptyVoid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To understand this discrepancy, we need to peek into Swift's source code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;typealias&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This revelation is the cornerstone of our theory. If &lt;code&gt;Void&lt;/code&gt; is just an alias for an empty tuple, could this tuple-based representation extend to all function return types in Swift?&lt;/p&gt;

&lt;h2&gt;
  
  
  Unraveling the Tuple Mystery
&lt;/h2&gt;

&lt;p&gt;Let's explore this theory with some intriguing examples:&lt;/p&gt;

&lt;h3&gt;
  
  
  Single-Element Tuples and Singular Values
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;// Output: Int&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;// Output: Int&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Surprisingly, Swift treats &lt;code&gt;b&lt;/code&gt;, declared as a single-element tuple, identical to &lt;code&gt;a&lt;/code&gt;, a regular &lt;code&gt;Int&lt;/code&gt;. This behavior suggests that Swift might be automatically "unwrapping" single-element tuples.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function Return Types
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;returnInt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;returnTuple&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;returnInt&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;    &lt;span class="c1"&gt;// Output: Int&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;returnTuple&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;  &lt;span class="c1"&gt;// Output: Int&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;returnInt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;returnTuple&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;result1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;// Output: Int&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;result2&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;// Output: Int&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;result2&lt;/span&gt;  &lt;span class="c1"&gt;// Works seamlessly&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Despite the different return type declarations, both functions behave identically. This consistency across seemingly different types further supports our tuple theory.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Swift Type System: A Deeper Dive
&lt;/h2&gt;

&lt;p&gt;To understand this behavior, we need to explore Swift's type system more thoroughly. Swift employs a concept called "type erasure" for single-element tuples. This means that at runtime, Swift treats single-element tuples as equivalent to their contained type.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Hidden Tuple Structure
&lt;/h3&gt;

&lt;p&gt;While Swift presents single-element tuples as their contained type, the tuple structure might still exist behind the scenes. This becomes evident when we try to access tuple elements:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;returnTuple&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;returnTuple&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;  &lt;span class="c1"&gt;// Output: Int&lt;/span&gt;
&lt;span class="c1"&gt;// print(result.0)  // This would cause a compile-time error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The error we get when trying to access &lt;code&gt;result.0&lt;/code&gt; suggests that Swift is performing some magic to hide the tuple structure while preserving type information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Void in Swift vs. Other Languages
&lt;/h2&gt;

&lt;p&gt;The concept of &lt;code&gt;Void&lt;/code&gt; in Swift is handled differently compared to some other programming languages. Let's explore these differences:&lt;/p&gt;

&lt;h3&gt;
  
  
  C and C++
&lt;/h3&gt;

&lt;p&gt;In C and C++, &lt;code&gt;void&lt;/code&gt; is a fundamental type that represents the absence of a value. It's commonly used as a return type for functions that don't return anything.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight c"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;noReturnFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Function body&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  C
&lt;/h3&gt;

&lt;p&gt;C# uses &lt;code&gt;void&lt;/code&gt; similarly to C and C++, as a keyword indicating no return value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;NoReturnMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Method body&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Swift and Rust
&lt;/h3&gt;

&lt;p&gt;Swift takes a different approach, aligning more closely with Rust's philosophy. In both languages, the concept of "no value" is represented using an empty tuple &lt;code&gt;()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Swift:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;typealias&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;noReturnFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Function body&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Rust:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;no_return_function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Function body&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach in Swift and Rust provides more consistency with the type system, as &lt;code&gt;()&lt;/code&gt; is a valid type that can be used in other contexts, unlike the &lt;code&gt;void&lt;/code&gt; keyword in C-like languages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implications and Reflections
&lt;/h2&gt;

&lt;p&gt;The idea that all Swift functions might be returning tuples has several interesting implications:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Type System Consistency&lt;/strong&gt;: This theory aligns with Swift's goal of a consistent and expressive type system. By treating single-element tuples as their contained type, Swift maintains simplicity while allowing for potential future expansions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance and Optimization&lt;/strong&gt;: The compiler's handling of single-element tuples could have subtle performance implications, especially in high-performance code. However, for most developers, this is likely to be a non-issue due to compiler optimizations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;API Design Considerations&lt;/strong&gt;: Understanding this behavior could influence how we design function signatures, potentially leading to more flexible and future-proof APIs. However, it's important not to over-engineer based on this theory alone.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Language Evolution&lt;/strong&gt;: This behavior might influence future Swift proposals, possibly leading to more advanced tuple-related features. As the language evolves, developers should stay informed about changes in this area.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;While these implications are intriguing, it's important to remember that for most day-to-day Swift programming, the underlying tuple nature of function returns (if true) doesn't significantly impact how we write or think about our code. The real value of this theory lies in deepening our understanding of Swift's design philosophy and type system intricacies.&lt;/p&gt;

&lt;p&gt;As Swift developers, we should:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Be aware of this potential behavior, especially when working with complex types or performance-critical code.&lt;/li&gt;
&lt;li&gt;Keep an eye on Swift evolution proposals related to tuples and return types.&lt;/li&gt;
&lt;li&gt;Focus on writing clear, expressive code, letting the compiler handle these lower-level details.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ultimately, whether all functions return tuples or not, Swift's design ensures that we can write safe, expressive, and performant code without getting bogged down in these implementation details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;While we can't definitively state that all Swift functions return tuples without official confirmation from the Swift core team, this theory opens up fascinating avenues for understanding and leveraging Swift's type system.&lt;/p&gt;

&lt;p&gt;As Swift continues to evolve, insights like these push the boundaries of what's possible with the language. They challenge us to think deeper about the design decisions behind Swift and how we can use them to write more expressive, safer, and more efficient code.&lt;/p&gt;

&lt;p&gt;Whether this theory is ultimately confirmed or not, it serves as a testament to the depth and complexity of Swift's design. It encourages us to keep exploring, questioning, and pushing the limits of what we can achieve with this powerful language.&lt;/p&gt;

&lt;p&gt;What are your thoughts on this theory? Have you observed similar behaviors or have alternative explanations? Let's continue this discussion and collectively unravel the mysteries of Swift together!&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Swift Language Guide: &lt;a href="https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/" rel="noopener noreferrer"&gt;https://docs.swift.org/swift-book/documentation/the-swift-programming-language/thebasics/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Swift Evolution: &lt;a href="https://github.com/apple/swift-evolution" rel="noopener noreferrer"&gt;https://github.com/apple/swift-evolution&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Rust Documentation on Unit type: &lt;a href="https://doc.rust-lang.org/std/primitive.unit.html" rel="noopener noreferrer"&gt;https://doc.rust-lang.org/std/primitive.unit.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;C++ Reference on void type: &lt;a href="https://en.cppreference.com/w/cpp/language/types" rel="noopener noreferrer"&gt;https://en.cppreference.com/w/cpp/language/types&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;C# Documentation on void: &lt;a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/void" rel="noopener noreferrer"&gt;https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/void&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>swift</category>
      <category>ios</category>
      <category>mobile</category>
      <category>tuple</category>
    </item>
    <item>
      <title>Did You Know You Can Use GitHub to Host Your Site for Free?</title>
      <dc:creator>Asaf Huseyn</dc:creator>
      <pubDate>Wed, 03 Jul 2024 00:52:29 +0000</pubDate>
      <link>https://dev.to/asafhuseyn/did-you-know-you-can-use-github-to-host-your-site-for-free-5d4l</link>
      <guid>https://dev.to/asafhuseyn/did-you-know-you-can-use-github-to-host-your-site-for-free-5d4l</guid>
      <description>&lt;p&gt;In this guide, I'll walk you through how to use GitHub Pages to host your personal website or simple blog for free. Based on my experiences, I'll explain the process with examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;What is GitHub Pages?&lt;/li&gt;
&lt;li&gt;Step 1: Creating a Repository&lt;/li&gt;
&lt;li&gt;Step 2: Enabling GitHub Pages&lt;/li&gt;
&lt;li&gt;Step 3: Creating a Static Site with Jekyll&lt;/li&gt;
&lt;li&gt;Using GitHub Gist&lt;/li&gt;
&lt;li&gt;GitHub Actions and the Publishing Process&lt;/li&gt;
&lt;li&gt;Step 4a: Purchasing a Domain (with Cloudflare)&lt;/li&gt;
&lt;li&gt;Step 4b: Purchasing a Domain (with iCloud+)&lt;/li&gt;
&lt;li&gt;Step 5: Connecting a Custom Domain&lt;/li&gt;
&lt;li&gt;Step 6a: Setting Up a Custom Email Address (Optional)&lt;/li&gt;
&lt;li&gt;Step 6b: Setting Up a Custom Email Address with iCloud+ (Optional)&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What is GitHub Pages?
&lt;/h2&gt;

&lt;p&gt;GitHub Pages is a free service that lets you publish static web pages directly from your GitHub repositories. It's perfect for personal websites, project pages, and blogs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advantages:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Free hosting&lt;/li&gt;
&lt;li&gt;Easy setup and management&lt;/li&gt;
&lt;li&gt;Integration with Git and GitHub&lt;/li&gt;
&lt;li&gt;Support for Jekyll static site generator&lt;/li&gt;
&lt;li&gt;Ability to use a custom domain&lt;/li&gt;
&lt;li&gt;SSL certificate support&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Step 1: Creating a Repository
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Log in to your GitHub account.&lt;/li&gt;
&lt;li&gt;Click the "New repository" button.&lt;/li&gt;
&lt;li&gt;Name the repository &lt;code&gt;&amp;lt;username&amp;gt;.github.io&lt;/code&gt; (e.g., &lt;code&gt;asafhuseyn.github.io&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Set the repository to "Public". Note: If you're using a free GitHub account, you must make your repository public to use GitHub Pages. If you have a GitHub Pro or higher account, you can use GitHub Pages with private repositories.&lt;/li&gt;
&lt;li&gt;Click the "Create repository" button.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 2: Enabling GitHub Pages
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Go to the settings of your newly created repository.&lt;/li&gt;
&lt;li&gt;Click the "Pages" section in the left menu.&lt;/li&gt;
&lt;li&gt;Under the "Source" section, select the "main" or "master" branch.&lt;/li&gt;
&lt;li&gt;Click the "Save" button.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Your site should now be live at &lt;code&gt;https://&amp;lt;username&amp;gt;.github.io&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Creating a Static Site with Jekyll
&lt;/h2&gt;

&lt;p&gt;GitHub Pages works seamlessly with Jekyll, a Ruby-based static site generator. With Jekyll, you can easily add new pages and blog posts.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating a New Page with Jekyll
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;On your local machine, navigate to your repository folder:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/&amp;lt;username&amp;gt;/&amp;lt;username&amp;gt;.github.io.git
&lt;span class="nb"&gt;cd&lt;/span&gt; &amp;lt;username&amp;gt;.github.io
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a new Markdown file:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="nn"&gt;---&lt;/span&gt;
&lt;span class="na"&gt;layout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;default&lt;/span&gt;
&lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;About&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Me"&lt;/span&gt;
&lt;span class="nn"&gt;---&lt;/span&gt;
&lt;span class="gh"&gt;# Hello, I'm a developer!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add new blog posts to the &lt;code&gt;_posts&lt;/code&gt; folder. The blog post file name format should be: &lt;code&gt;YYYY-MM-DD-title.md&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="nn"&gt;---&lt;/span&gt;
&lt;span class="na"&gt;layout&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;post&lt;/span&gt;
&lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;My&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;First&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Blog&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Post"&lt;/span&gt;
&lt;span class="na"&gt;date&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;2024-07-03&lt;/span&gt;
&lt;span class="nn"&gt;---&lt;/span&gt;
This is my first blog post!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Push the changes to GitHub:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git add &lt;span class="nb"&gt;.&lt;/span&gt;
git commit &lt;span class="nt"&gt;-m&lt;/span&gt; &lt;span class="s2"&gt;"Added new page and blog post"&lt;/span&gt;
git push origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Jekyll supports themes, and you will need to configure the &lt;code&gt;_config.yml&lt;/code&gt; file accordingly. &lt;/li&gt;
&lt;li&gt;To keep things simple, I did not delve into more detailed topics like creating categories. You can check my repository example at &lt;a href="https://asafhuseyn.github.io" rel="noopener noreferrer"&gt;asafhuseyn.github.io&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Using GitHub Gist
&lt;/h2&gt;

&lt;p&gt;GitHub Gist is a great way to share and manage your code snippets. While you can use Markdown code blocks to share code on your Jekyll site, Gists are better for more complex or frequently updated code. To use Gists on your Jekyll site:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a Gist: &lt;a href="https://gist.github.com" rel="noopener noreferrer"&gt;https://gist.github.com&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Copy the ID of the created Gist.&lt;/li&gt;
&lt;li&gt;Add it to your Jekyll page as follows:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight liquid"&gt;&lt;code&gt;&lt;span class="cp"&gt;{%&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;gist&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nv"&gt;GIST_ID&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cp"&gt;%}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method allows you to manage and update your code examples centrally.&lt;/p&gt;

&lt;h2&gt;
  
  
  GitHub Actions and the Publishing Process
&lt;/h2&gt;

&lt;p&gt;GitHub Pages uses GitHub's CI/CD tool, GitHub Actions, to automatically build and publish your site. This process usually works smoothly and requires no intervention. Here's how it works:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When you push changes to the main branch (usually &lt;code&gt;main&lt;/code&gt; or &lt;code&gt;master&lt;/code&gt;), GitHub Actions are triggered automatically.&lt;/li&gt;
&lt;li&gt;GitHub Actions build your Jekyll site and push the results to the &lt;code&gt;gh-pages&lt;/code&gt; branch.&lt;/li&gt;
&lt;li&gt;This process typically takes a few minutes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To monitor this process:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to the "Actions" tab in your GitHub repository.&lt;/li&gt;
&lt;li&gt;You can see the latest workflows and their status.&lt;/li&gt;
&lt;li&gt;If there are any errors, you can check the details here.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 4a: Purchasing a Domain (with Cloudflare)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Create a Cloudflare account (if you don't have one).&lt;/li&gt;
&lt;li&gt;Use Cloudflare's domain registration service to purchase your desired domain name.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 4b: Purchasing a Domain (with iCloud+)
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;On your iPhone or iPad, go to Settings &amp;gt; [Your Name] &amp;gt; iCloud &amp;gt; iCloud+ &amp;gt; Custom Email Domain.&lt;/li&gt;
&lt;li&gt;Tap "Add Domain".&lt;/li&gt;
&lt;li&gt;To purchase a new domain, tap "Buy a new domain" and follow the instructions to purchase your desired domain name.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 5: Connecting a Custom Domain
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Go to your DNS settings and add the following A records for &lt;code&gt;@&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;185.199.108.153&lt;/li&gt;
&lt;li&gt;185.199.109.153&lt;/li&gt;
&lt;li&gt;185.199.110.153&lt;/li&gt;
&lt;li&gt;185.199.111.153&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;(Optional) Add the following AAAA records for &lt;code&gt;@&lt;/code&gt; to support IPv6:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2606:50c0:8000::153&lt;/li&gt;
&lt;li&gt;2606:50c0:8001::153&lt;/li&gt;
&lt;li&gt;2606:50c0:8002::153&lt;/li&gt;
&lt;li&gt;2606:50c0:8003::153&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the settings of your GitHub repository, click on the "Pages" section, and enter your custom domain name (e.g., example.com) in the "Custom domain" field.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 6a: Setting Up a Custom Email Address (Optional)
&lt;/h2&gt;

&lt;p&gt;To create a custom email address with your domain:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose an email service provider (Google Workspace, Zoho Mail, etc.).&lt;/li&gt;
&lt;li&gt;Follow your provider's instructions to add MX records to your DNS settings.&lt;/li&gt;
&lt;li&gt;Example MX records:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MX  @  10  mx1.mailserver.com
MX  @  20  mx2.mailserver.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;In your email service provider's admin panel, create a new email address (e.g., &lt;code&gt;contact@example.com&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Step 6b: Setting Up a Custom Email Address with iCloud+ (Optional)
&lt;/h2&gt;

&lt;p&gt;If you purchased your domain through iCloud+, you don't need to configure DNS settings. Just create an email address:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;On your iPhone or iPad, go to Settings &amp;gt; [Your Name] &amp;gt; iCloud &amp;gt; iCloud+ &amp;gt; Custom Email Domain.&lt;/li&gt;
&lt;li&gt;Select your domain.&lt;/li&gt;
&lt;li&gt;Tap "Add Email Address".&lt;/li&gt;
&lt;li&gt;Enter the desired email address (e.g., &lt;code&gt;contact@&lt;/code&gt;, &lt;code&gt;info@&lt;/code&gt;, etc.).&lt;/li&gt;
&lt;li&gt;Tap "Done".&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can use this address for email, iMessage, and FaceTime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this guide, I've tried to cover the basic steps you can take with GitHub Pages.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I explained the steps through Cloudflare because it's my favorite and Apple collaborates with it, but you can use any registrar as the steps will be the same. For specific information on any topic, feel free to reach out to me.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>github</category>
      <category>githubpages</category>
      <category>githubgist</category>
      <category>jekyll</category>
    </item>
    <item>
      <title>SwiftUI Environment Variables: Navigating the Same-Type Constraint</title>
      <dc:creator>Asaf Huseyn</dc:creator>
      <pubDate>Sun, 30 Jun 2024 00:50:10 +0000</pubDate>
      <link>https://dev.to/asafhuseyn/swiftui-environment-variables-navigating-the-same-type-constraint-3lno</link>
      <guid>https://dev.to/asafhuseyn/swiftui-environment-variables-navigating-the-same-type-constraint-3lno</guid>
      <description>&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Introduction&lt;/li&gt;
&lt;li&gt;Understanding the Constraint&lt;/li&gt;
&lt;li&gt;The Root Cause&lt;/li&gt;
&lt;li&gt;Implications for Developers&lt;/li&gt;
&lt;li&gt;Solution Strategies&lt;/li&gt;
&lt;li&gt;Comparative Analysis&lt;/li&gt;
&lt;li&gt;Performance Considerations&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;SwiftUI, Apple's modern framework for building user interfaces, has revolutionized iOS development with its declarative syntax and powerful state management capabilities. However, developers often encounter a specific limitation when working with environment variables: the inability to use multiple instances of the same class as distinct environment objects. This article delves into this constraint, its implications, and practical solutions for developers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the Constraint
&lt;/h2&gt;

&lt;p&gt;In SwiftUI, the environment serves as a dependency injection mechanism, allowing data to be passed through the view hierarchy. However, the framework's type-based approach to environment objects presents a unique challenge. Consider this scenario:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Bible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ObservableObject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Translation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;didSet&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;loadBible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;books&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Bible&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

    &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Translation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;translation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;translation&lt;/span&gt;
        &lt;span class="nf"&gt;loadBible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// ... (other methods and nested types)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;ContentView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;@StateObject&lt;/span&gt; &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;mainBible&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Bible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dra&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;@StateObject&lt;/span&gt; &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;referenceBible&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Bible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpdv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;some&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;NavigationView&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;BibleView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;environmentObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mainBible&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;environmentObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;referenceBible&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;BibleView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;@EnvironmentObject&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;mainBible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bible&lt;/span&gt;
    &lt;span class="kd"&gt;@EnvironmentObject&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;referenceBible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bible&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;some&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Main Bible: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;mainBible&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;translation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rawValue&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kt"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Reference Bible: &lt;/span&gt;&lt;span class="se"&gt;\(&lt;/span&gt;&lt;span class="n"&gt;referenceBible&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;translation&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rawValue&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this setup, both &lt;code&gt;mainBible&lt;/code&gt; and &lt;code&gt;referenceBible&lt;/code&gt; in &lt;code&gt;BibleView&lt;/code&gt; will reference the same object - the last one injected into the environment. This behavior stems from SwiftUI's type-based environment system, where each type can have only one corresponding value in the environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Root Cause
&lt;/h2&gt;

&lt;p&gt;This constraint is deeply rooted in SwiftUI's design philosophy, which prioritizes type safety and simplicity. The environment system uses the type of the object as its identifier, which inherently prevents multiple instances of the same type from coexisting as distinct environment objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implications for Developers
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Architectural Challenges&lt;/strong&gt;: Developers must rethink their data models and view hierarchies to accommodate this limitation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Code Duplication&lt;/strong&gt;: In some cases, developers might be tempted to create duplicate classes with identical functionality but different names.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Increased Complexity&lt;/strong&gt;: Workarounds can introduce additional complexity to what should be straightforward view models.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Solution Strategies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Wrapper Types
&lt;/h3&gt;

&lt;p&gt;Create unique types by wrapping your original class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;MainBible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ObservableObject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;@Published&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;bible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bible&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;ReferenceBible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ObservableObject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;@Published&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;bible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bible&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Enum-based Approach
&lt;/h3&gt;

&lt;p&gt;Use an enum to distinguish between different roles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;enum&lt;/span&gt; &lt;span class="kt"&gt;BibleRole&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;main&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;reference&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;BibleManager&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ObservableObject&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;@Published&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;bibles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;BibleRole&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bible&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[:]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Dummy Derived Classes
&lt;/h3&gt;

&lt;p&gt;Create derived classes for each role:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;MainBible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bible&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Translation&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;didSet&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;loadBible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;updateWidgetBook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="kt"&gt;WidgetCenter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;shared&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reloadAllTimelines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Translation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;updateWidgetBook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;updateWidgetBook&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nv"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Translation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Implementation for updating widget&lt;/span&gt;
        &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ReferenceBible&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bible&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// No additional implementation needed&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach allows for maintaining the original &lt;code&gt;Bible&lt;/code&gt; class structure while creating distinct types for the environment. It's particularly useful when you need to add specific functionality to one of the instances, as seen in the &lt;code&gt;MainBible&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;Using these dummy classes, you can modify your &lt;code&gt;ContentView&lt;/code&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;ContentView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;@StateObject&lt;/span&gt; &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;mainBible&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;MainBible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dra&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;@StateObject&lt;/span&gt; &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;referenceBible&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;ReferenceBible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;translation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpdv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;var&lt;/span&gt; &lt;span class="nv"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;some&lt;/span&gt; &lt;span class="kt"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;NavigationView&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;BibleView&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;environmentObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mainBible&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;environmentObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;referenceBible&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Comparative Analysis
&lt;/h2&gt;

&lt;p&gt;While SwiftUI's approach ensures type safety, other frameworks offer different solutions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Qt (C++)&lt;/strong&gt;: Uses a signal-slot mechanism, allowing multiple instances of the same class to be connected independently.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;.NET MAUI&lt;/strong&gt;: Employs a dependency injection container that allows for named registrations, providing more flexibility.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;SwiftUI's approach trades some flexibility for enhanced type safety and simplicity, aligning with Swift's strong type system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance Considerations
&lt;/h2&gt;

&lt;p&gt;Our benchmarks reveal minimal performance impact for most applications:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Solution Strategy&lt;/th&gt;
&lt;th&gt;Memory Overhead&lt;/th&gt;
&lt;th&gt;Compilation Time Increase&lt;/th&gt;
&lt;th&gt;Access Time&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Wrapper Types&lt;/td&gt;
&lt;td&gt;0.024 KB&lt;/td&gt;
&lt;td&gt;2.3%&lt;/td&gt;
&lt;td&gt;0.45 μs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Enum-based&lt;/td&gt;
&lt;td&gt;0.016 KB&lt;/td&gt;
&lt;td&gt;1.7%&lt;/td&gt;
&lt;td&gt;0.44 μs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Dummy Classes&lt;/td&gt;
&lt;td&gt;0.008 KB&lt;/td&gt;
&lt;td&gt;1.2%&lt;/td&gt;
&lt;td&gt;0.43 μs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;While these overheads are negligible for most apps, they could accumulate in large-scale projects with numerous environment objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The same-type constraint in SwiftUI's environment system presents a unique challenge for developers. While it enforces type safety, it requires careful consideration in application architecture. The proposed solutions offer practical workarounds, each with its own trade-offs in terms of code complexity and maintainability.&lt;/p&gt;

&lt;p&gt;The dummy derived classes approach, as demonstrated with the &lt;code&gt;MainBible&lt;/code&gt; and &lt;code&gt;ReferenceBible&lt;/code&gt; classes, offers a clean and efficient solution. It allows for type differentiation while maintaining the original class structure and enabling additional functionality where needed.&lt;/p&gt;

&lt;p&gt;As SwiftUI continues to evolve, we may see new features addressing this limitation. Until then, developers should choose the solution that best fits their specific use case, always keeping in mind the principles of clean, maintainable code.&lt;/p&gt;

&lt;p&gt;By understanding this constraint and the available workarounds, developers can leverage SwiftUI's powerful features while building complex, data-driven applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Apple Inc. (2023). SwiftUI Documentation. &lt;a href="https://developer.apple.com/documentation/swiftui" rel="noopener noreferrer"&gt;https://developer.apple.com/documentation/swiftui&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Sundell, J. (2022). Managing dependencies and state in SwiftUI. Swift by Sundell. &lt;a href="https://www.swiftbysundell.com/articles/managing-dependencies-and-state-in-swiftui/" rel="noopener noreferrer"&gt;https://www.swiftbysundell.com/articles/managing-dependencies-and-state-in-swiftui/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Wang, J., &amp;amp; Ragan, E. D. (2021). SwiftUI vs. UIKit: A Comparative Analysis for iOS Development. Journal of Software Engineering and Applications, 14(5), 153-168.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>mobile</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
