<?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>Forem: Florian Schliep</title>
    <description>The latest articles on Forem by Florian Schliep (@floschliep).</description>
    <link>https://forem.com/floschliep</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%2F39211%2F2f9e2375-0499-4604-85a6-b87d38f020b3.jpeg</url>
      <title>Forem: Florian Schliep</title>
      <link>https://forem.com/floschliep</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/floschliep"/>
    <language>en</language>
    <item>
      <title>Experiment: Lazy Objects in Swift</title>
      <dc:creator>Florian Schliep</dc:creator>
      <pubDate>Fri, 16 Mar 2018 10:55:16 +0000</pubDate>
      <link>https://forem.com/floschliep/experiment-lazy-objects-in-swift--1303</link>
      <guid>https://forem.com/floschliep/experiment-lazy-objects-in-swift--1303</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;NEW: This blog post is also available on my &lt;a href="https://floschliep.com/blog/2018-03-15-experiment-lazy-objects-in-swift"&gt;personal website&lt;/a&gt; alongside even more content!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: This post is purely experimental based on an idea I had recently, I’m personally not using this in production right now.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Laziness in Swift can be a very powerful tool, but it hasn’t reached its full potential yet. Recently, I came across a situation where I &lt;em&gt;wanted&lt;/em&gt; to write something likes this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In other words: I wanted to pass the closure for creating a lazy property to the initializer of my class &lt;em&gt;and&lt;/em&gt; make it a constant property. This doesn’t work because of two reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Properties are being evaluated upon instantiation of the object, except for lazy variables. &lt;code&gt;lazy let&lt;/code&gt; doesn’t exist in Swift.&lt;/li&gt;
&lt;li&gt;Lazy properties must declare their initializer, meaning you can’t pass a closure as the initializer to it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With those constraints, our only option is to use existing features to make this work. Here’s what I expect from my implementation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pass a closure, which won’t be evaluated until it’s necessary, to &lt;em&gt;something&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;The closure should only be &lt;em&gt;evaluated once&lt;/em&gt;, otherwise this would defeat the purpose of the whole concept as we could just use a normal closure.&lt;/li&gt;
&lt;li&gt;This should be universally usable with &lt;em&gt;anything&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;A readable, easy-to-understand syntax.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No 3 will be easy to satisfy by using Generics. Due to No 1 &amp;amp; 2 it will be necessary to use a &lt;code&gt;class&lt;/code&gt;, as evaluating and storing the result of our closure implicitly mutates the object.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Using our &lt;code&gt;LazyObject&lt;/code&gt; wrapper now looks like this:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Only on the last line the closure will actually be evaluated and our &lt;code&gt;Bar&lt;/code&gt; object created. Calling &lt;code&gt;bar.object&lt;/code&gt; again would just return the cached object and not evaluate the closure again.&lt;/p&gt;

&lt;p&gt;So far so good. What about structs though? Right now, our wrapper doesn’t allow mutating the underlying &lt;code&gt;object&lt;/code&gt;. Let’s create a mutable subclass, &lt;code&gt;MutableLazyObject&lt;/code&gt;, that provides a scope allowing mutation:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;By moving the &lt;code&gt;use(_:)&lt;/code&gt; method to a subclass, we can explicitly decide whether we want to allow mutation or not.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Unfortunately though, using the &lt;code&gt;LazyObject&lt;/code&gt; class doesn’t look nice, violating expectation No 4. Let’s write a short helper function:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This function allows us to use the shorthand syntax &lt;code&gt;lazy(Bar())&lt;/code&gt;. Much better!&lt;/p&gt;




&lt;p&gt;The full code, including tests, is available on &lt;a href="https://github.com/floschliep/LazyObject"&gt;GitHub&lt;/a&gt;. I’d love to get some feedback on this! Is this a good idea? Why or why not? Should Swift allow this kind of functionality on the language level?&lt;/p&gt;

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