{"id":49708,"date":"2017-08-01T09:31:28","date_gmt":"2017-08-01T04:01:28","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=49708"},"modified":"2022-01-13T14:11:15","modified_gmt":"2022-01-13T08:41:15","slug":"recursive-enumerations-in-swift","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/recursive-enumerations-in-swift\/","title":{"rendered":"Recursive Enumerations in Swift"},"content":{"rendered":"<p class=\"p1\"><strong>Why use Recursion?<\/strong><\/p>\n<ul>\n<li class=\"p3\">When we say recursion, we are referring to a technique where an object is referring to itself.<\/li>\n<li class=\"p3\">They&#8217;re useful<b> where you need to do the representation of hierarchical structure like trees, network\/graphs<\/b>, which otherwise would be complicated to be represented using loops.<\/li>\n<li class=\"p3\"><b>It is possible to achieve recursion technique with reference types(class) since reference stores their instances.<\/b><\/li>\n<li class=\"p3\">Because value types are copied unlike reference types, we cannot use them to achieve recursion.<\/li>\n<li class=\"p3\">Since Swift made value types like <span class=\"s1\"><code>struct,\u00a0<\/code><\/span><code>enum <\/code>a first class citizen, it introduces a possibility of handling recursion with value type using a recursive enum.<\/li>\n<\/ul>\n<p class=\"p1\"><strong>What is Recursive Enum?<\/strong><\/p>\n<ul>\n<li class=\"p1\">Recursive Enums works on the above-mentioned principle of recursion.<\/li>\n<li>Let&#8217;s consider <strong>an example of representing Family Tree using the recursive enum<\/strong>. The scenario is shown in the figure below:<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-49815 size-full alignnone aligncenter\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/06\/Screen-Shot-2017-06-28-at-10.45.00-AM.png\" alt=\"\" width=\"1195\" height=\"602\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/06\/Screen-Shot-2017-06-28-at-10.45.00-AM.png 1195w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/Screen-Shot-2017-06-28-at-10.45.00-AM-300x151.png 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/Screen-Shot-2017-06-28-at-10.45.00-AM-1024x515.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/Screen-Shot-2017-06-28-at-10.45.00-AM-624x314.png 624w\" sizes=\"(max-width: 1195px) 100vw, 1195px\" \/><\/p>\n<ul>\n<li><strong>In recursive enumeration, one or more cases have the type of an associated value to be the enumeration itself.<\/strong><\/li>\n<\/ul>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nindirect enum FamilyTree {<br \/>\n\tcase noKnownParents<br \/>\n\tcase oneKnownParent(name: String, ancestors: FamilyTreeOne)<br \/>\n\tcase twoKnownParents(fatherName: String, fatherAncestors: FamilyTreeOne,<br \/>\n\tmotherName: String, motherAncestors: FamilyTreeOne)<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Or another syntax<\/p>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nenum FamilyTree {<br \/>\n\tcase noKnownParents<br \/>\n\tindirect case oneKnownParent(name: String, ancestors: FamilyTreeTwo)<br \/>\n\tindirect case twoKnownParents(fatherName: String, fatherAncestors: FamilyTreeTwo,<br \/>\n\tmotherName: String, motherAncestors: FamilyTreeTwo)<br \/>\n}<br \/>\n[\/code]<\/p>\n<p><span style=\"text-decoration: underline\"><strong>The indirect keyword<\/strong><\/span><\/p>\n<ul>\n<li>Recursive enums require the keyword\u00a0<code>indirect<\/code> either before enum keyword or before the specific case.<\/li>\n<\/ul>\n<p>So what&#8217;s the deal with this keyword\u00a0indirect? Let&#8217;s understand how it works:<\/p>\n<ul>\n<li>For every instance created for a type in Swift, <strong>the compiler requires the knowledge of how much memory will it occupy.<\/strong><\/li>\n<li>For a Swift enum<strong>, an\u00a0instance of an enum will only have one case assigned to itself\u00a0<\/strong>(which may change as your code runs) at any given point of time.<\/li>\n<li>The <strong>compiler now checks which case of the enum will occupy the maximum memory.<\/strong><\/li>\n<li>Hence, the\u00a0instance of enum created will have the<strong> required memory plus any additional requirement by the compiler for tracking which case is currently assigned.<\/strong><\/li>\n<\/ul>\n<p>Consider the below\u00a0example<span style=\"background-color: #f5f6f5\">\u00a0<\/span>:<\/p>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nenum Area {<br \/>\n\tcase square(side:Double)<br \/>\n\tcase rectangle(length: Double, breadth: Double)<br \/>\n}<br \/>\n[\/code]<\/p>\n<ul>\n<li>Since the case\u00a0<code>square<\/code> has one associated value, the amount of the memory required to create an instance will be one Double\u2019s worth of memory with any additional memory for tracking by the compiler.<\/li>\n<li>Similarly, the\u00a0instance of the case\u00a0<code>rectangle<\/code> will have the total memory \u00a016 bytes + any additional memory for tracking.<\/li>\n<li>Now in <code>FamilyTree<\/code>\u00a0 enum, the memory required for creating an instance of the case\u00a0<code>oneKnownParent<\/code> will be the\u00a0memory to hold a \u00a0String and an instance of\u00a0<code>FamilyTree.<\/code><\/li>\n<li><strong>The compiler cannot determine how big a<\/strong> <code>FamilyTree<\/code>\u00a0<strong>is <\/strong>or we could say that\u00a0<code>FamilyTree<\/code><strong>\u00a0would require an infinite amount of memory.<\/strong><\/li>\n<li>For this reason, <strong>we use the keyword<\/strong> <code>indirect <\/code><strong>which instructs the compiler to store the enum\u2019s data behind a pointer.<\/strong><\/li>\n<li><strong>The compiler now puts the data somewhere else in memory and creates a pointer to the associated data rather than making the instance of<\/strong> <code>FamilyTree<\/code> <strong>big enough to hold the data.<\/strong><\/li>\n<\/ul>\n<p style=\"padding-left: 30px\"><strong><span style=\"text-decoration: underline\">Example:<\/span><\/strong> Now let&#8217;s implement our above Family Tree by creating an instance of enum\u00a0<code>FamilyTree<\/code><\/p>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nlet fredAncestors = FamilyTree.twoKnownParents(<br \/>\n    fatherName: &quot;Fred Sr.&quot;,<br \/>\n    fatherAncestors: .oneKnownParent(name: &quot;Beth&quot;, ancestors: .noKnownParents),<br \/>\n    motherName: &quot;Marsha&quot;,<br \/>\n    motherAncestors: .noKnownParents)<br \/>\n[\/code]<\/p>\n<p style=\"padding-left: 30px\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-49816 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2017\/06\/Screen-Shot-2017-06-28-at-10.49.49-AM.png\" alt=\"Screen Shot 2017-06-28 at 10.49.49 AM\" width=\"1035\" height=\"549\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2017\/06\/Screen-Shot-2017-06-28-at-10.49.49-AM.png 1035w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/Screen-Shot-2017-06-28-at-10.49.49-AM-300x159.png 300w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/Screen-Shot-2017-06-28-at-10.49.49-AM-1024x543.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2017\/06\/Screen-Shot-2017-06-28-at-10.49.49-AM-624x330.png 624w\" sizes=\"(max-width: 1035px) 100vw, 1035px\" \/><\/p>\n<ul>\n<li class=\"p1\">If you check the\u00a0<b>size of the instance <code>fredAncestors,<\/code><\/b><span class=\"Apple-converted-space\">\u00a0<strong>it&#8217;<\/strong><\/span><b>s\u00a08 bytes on a 64-bit architecture \u2013 the size of one pointer.<\/b><\/li>\n<\/ul>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nMemoryLayout.size(ofValue:fredAncestors)<br \/>\n[\/code]<\/p>\n<p style=\"padding-left: 30px\"><strong><span style=\"text-decoration: underline\">Example<\/span>:\u00a0<\/strong>Another common example of recursive enum is doing the arithmetic operation as shown below:<\/p>\n<p>[code language=&#8221;objc&#8221;]<br \/>\nindirect enum ArithmeticExpressions {<br \/>\n\tcase number(Int)<br \/>\n\tcase addition(ArithmeticExpressions, ArithmeticExpressions)<br \/>\n\tcase multiplication(ArithmeticExpressions, ArithmeticExpressions)<br \/>\n}<br \/>\nfunc evaluate(_ expression: ArithmeticExpressions) -&gt; Int<br \/>\n\tswitch expression {<br \/>\n\t\tcase let .number(value):<br \/>\n\t\t\treturn value<br \/>\n\t\tcase let .addition(left, right):<br \/>\n\t\t\treturn evaluate(left) + evaluate(right)<br \/>\n\t\tcase let .multiplication(left, right):<br \/>\n\t\t\treturn evaluate(left) * evaluate(right)<br \/>\n\t}<br \/>\n}<br \/>\nlet multiplicationEnumType = ArithmeticExpressions.multiplication(.number(4), .number(4))<br \/>\nevaluate(multiplicationEnumType)<br \/>\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Why use Recursion? When we say recursion, we are referring to a technique where an object is referring to itself. They&#8217;re useful where you need to do the representation of hierarchical structure like trees, network\/graphs, which otherwise would be complicated to be represented using loops. It is possible to achieve recursion technique with reference types(class) [&hellip;]<\/p>\n","protected":false},"author":1072,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"image","meta":{"iawp_total_views":103},"categories":[1400,1994,1],"tags":[4848,4690,4621,4689,2715,3722,4534],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/49708"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1072"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=49708"}],"version-history":[{"count":1,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/49708\/revisions"}],"predecessor-version":[{"id":54646,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/49708\/revisions\/54646"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=49708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=49708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=49708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}