{"id":3083,"date":"2023-04-20T06:44:36","date_gmt":"2023-04-20T01:14:36","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3083"},"modified":"2023-04-20T06:44:36","modified_gmt":"2023-04-20T01:14:36","slug":"defer-statement-in-v-language","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/","title":{"rendered":"Defer Statement in V Language"},"content":{"rendered":"<p>The <a href=\"https:\/\/github.com\/vlang\/v\/blob\/master\/doc\/docs.md#defer\" target=\"_blank\" rel=\"noopener\"><em><strong>defer<\/strong><\/em><\/a> statement in <a href=\"https:\/\/vlang.io\/\" target=\"_blank\" rel=\"noopener\"><em><strong>Vlang<\/strong><\/em><\/a> causes code to be executed when the enclosing function returns. This feature is inspired by <a href=\"https:\/\/go.dev\/tour\/flowcontrol\/12\" target=\"_blank\" rel=\"noopener\"><em><strong>Golang<\/strong><\/em><\/a>, but is slightly more flexible since it allows any block of code to be associated with <em><strong>defer<\/strong><\/em> instead of just a function call.<\/p>\n<p><em><strong>defer<\/strong><\/em> is used primarily to handle <em><strong>\u201cclean up\u201d<\/strong><\/em> logic, such as closing files, etc. For those who are familiar with <em><strong>C++<\/strong><\/em>, <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/language\/raii\" target=\"_blank\" rel=\"noopener\"><em><strong>RAII<\/strong><\/em><\/a> is the common idiom to implement acquisition of resources in <em><strong>constructors<\/strong><\/em> and releasing them in <em><strong>destructors<\/strong><\/em>. In <em><strong>Vlang<\/strong><\/em> where such a luxury is not available, <em><strong>defer<\/strong><\/em> statement comes in handy for freeing resources. Of course, any logic can go into the <em><strong>defer<\/strong><\/em> block.<\/p>\n<p>Let us look at some examples to understand how <em><strong>defer<\/strong><\/em> can be used.<\/p>\n<figure id=\"attachment_3084\" aria-describedby=\"caption-attachment-3084\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"3084\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/example1-31\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1.jpg\" data-orig-size=\"1083,528\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681896006&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"The &amp;#8220;defer&amp;#8221; Statement\" data-image-description=\"&lt;p&gt;The &amp;#8220;defer&amp;#8221; Statement&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The &amp;#8220;defer&amp;#8221; Statement&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1-1024x499.jpg\" class=\"wp-image-3084\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1.jpg?resize=560%2C273&#038;ssl=1\" alt=\"The &quot;defer&quot; Statement\" width=\"560\" height=\"273\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1.jpg?resize=300%2C146&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1.jpg?resize=1024%2C499&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1.jpg?resize=768%2C374&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1.jpg?w=1083&amp;ssl=1 1083w\" sizes=\"(max-width: 560px) 100vw, 560px\" \/><\/a><figcaption id=\"caption-attachment-3084\" class=\"wp-caption-text\"><strong>The &#8220;defer&#8221; Statement<\/strong><\/figcaption><\/figure>\n<p>In the above example, the <em><strong>defer<\/strong><\/em> block contains two statements, one to write some text into the trace file and the other to close the file. Even though this block appears <em><strong>\u201clexically\u201d<\/strong><\/em> in the beginning of the function, it will be executed only when the function <em><strong>\u201creturns\u201d<\/strong><\/em>. In other words, it will be executed either after<em><strong> line 19<\/strong><\/em>, or after <em><strong>line 23<\/strong><\/em>, depending on the actual path taken. In this case, the argument to the function determines the actual path taken. Let us see this in action.<\/p>\n<p>Here is the <em><strong>main<\/strong><\/em> function:<\/p>\n<figure id=\"attachment_3086\" aria-describedby=\"caption-attachment-3086\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3086\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/main1-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main1.jpg\" data-orig-size=\"433,124\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681895044&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"The Main Function\" data-image-description=\"&lt;p&gt;The Main Function&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The Main Function&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main1.jpg\" class=\"size-medium wp-image-3086\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main1.jpg?resize=300%2C86&#038;ssl=1\" alt=\"The Main Function\" width=\"300\" height=\"86\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main1.jpg?resize=300%2C86&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main1.jpg?w=433&amp;ssl=1 433w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-3086\" class=\"wp-caption-text\"><strong>The Main Function<\/strong><\/figcaption><\/figure>\n<p>Here we are calling the function by passing <em><strong>\u201cfalse\u201d<\/strong><\/em> as argument. As a result, the <em><strong>\u201cif\u201d<\/strong><\/em> condition succeeds and the function returns after <em><strong>line 19<\/strong><\/em>. At this point, our <em><strong>defer block<\/strong><\/em> will be executed.<\/p>\n<figure id=\"attachment_3087\" aria-describedby=\"caption-attachment-3087\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1a.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3087\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/output1a\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1a.jpg\" data-orig-size=\"647,223\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681894942&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"The Output\" data-image-description=\"&lt;p&gt;The Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1a.jpg\" class=\"wp-image-3087\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1a.jpg?resize=450%2C155&#038;ssl=1\" alt=\"The Output\" width=\"450\" height=\"155\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1a.jpg?resize=300%2C103&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1a.jpg?w=647&amp;ssl=1 647w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-3087\" class=\"wp-caption-text\"><strong>The Output<\/strong><\/figcaption><\/figure>\n<p>A look at the trace file shows that the <em><strong>defer<\/strong><\/em> block is executed <em><strong>after<\/strong><\/em> the <em><strong>\u201cif\u201d<\/strong><\/em> block code.<br \/>\nLet us call the same function now, but passing <em><strong>\u201ctrue\u201d<\/strong><\/em> this time:<\/p>\n<figure id=\"attachment_3088\" aria-describedby=\"caption-attachment-3088\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3088\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/main2-3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main2.jpg\" data-orig-size=\"383,150\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681895095&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Passing &amp;#8220;true&amp;#8221; as Argument\" data-image-description=\"&lt;p&gt;Passing &amp;#8220;true&amp;#8221; as Argument&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Passing &amp;#8220;true&amp;#8221; as Argument&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main2.jpg\" class=\"size-medium wp-image-3088\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main2.jpg?resize=300%2C117&#038;ssl=1\" alt=\"Passing &quot;true&quot; as Argument\" width=\"300\" height=\"117\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main2.jpg?resize=300%2C117&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main2.jpg?w=383&amp;ssl=1 383w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-3088\" class=\"wp-caption-text\"><strong>Passing &#8220;true&#8221; as Argument<\/strong><\/figcaption><\/figure>\n<p>Here is the corresponding trace:<\/p>\n<figure id=\"attachment_3089\" aria-describedby=\"caption-attachment-3089\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1b.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3089\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/output1b\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1b.jpg\" data-orig-size=\"581,182\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681895170&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1b.jpg\" class=\"wp-image-3089\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1b.jpg?resize=450%2C141&#038;ssl=1\" alt=\"Program Output\" width=\"450\" height=\"141\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1b.jpg?resize=300%2C94&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output1b.jpg?w=581&amp;ssl=1 581w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-3089\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>This time, the <em><strong>\u201cif\u201d<\/strong><\/em> block is bypassed and <em><strong>line 23<\/strong> <\/em>is executed, followed by <em><strong>defer<\/strong><\/em> block.<\/p>\n<p>This example demonstrates how the <em><strong>defer<\/strong><\/em> statement can be used to implement <em><strong>\u201cclean up\u201d<\/strong><\/em> logic just before function return.<\/p>\n<h3>Multiple \u201cdefer\u201d Statements<\/h3>\n<p>Is it possible to define multiple <em><strong>defer<\/strong><\/em> statements inside a function? The answer is <em><strong>\u201cyes\u201d<\/strong><\/em>. Take a look at the following code:<\/p>\n<figure id=\"attachment_3090\" aria-describedby=\"caption-attachment-3090\" style=\"width: 375px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3090\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/example2-27\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example2.jpg\" data-orig-size=\"530,370\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681895223&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Multiple &amp;#8220;defer&amp;#8221; Statements\" data-image-description=\"&lt;p&gt;Multiple &amp;#8220;defer&amp;#8221; Statements&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Multiple &amp;#8220;defer&amp;#8221; Statements&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example2.jpg\" class=\"wp-image-3090\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example2.jpg?resize=375%2C262&#038;ssl=1\" alt=\"Multiple &quot;defer&quot; Statements\" width=\"375\" height=\"262\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example2.jpg?resize=300%2C209&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example2.jpg?w=530&amp;ssl=1 530w\" sizes=\"(max-width: 375px) 100vw, 375px\" \/><\/a><figcaption id=\"caption-attachment-3090\" class=\"wp-caption-text\"><strong>Multiple &#8220;defer&#8221; Statements<\/strong><\/figcaption><\/figure>\n<p>In the above example, we have two <em><strong>defer<\/strong><\/em> statements inside the function. In addition, the function returns a value to the caller. When multiple <em><strong>defer<\/strong><\/em> statements are present, these statements will be executed in the <em><strong>\u201creverse\u201d<\/strong><\/em> order and the returned value will be made available to the caller (we are not using the returned value). Here is the <em><strong>\u201cmain\u201d<\/strong><\/em> function:<\/p>\n<figure id=\"attachment_3092\" aria-describedby=\"caption-attachment-3092\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3092\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/main3\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main3.jpg\" data-orig-size=\"402,158\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681895609&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Calling from Main\" data-image-description=\"&lt;p&gt;Calling from Main&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Calling from Main&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main3.jpg\" class=\"size-medium wp-image-3092\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main3.jpg?resize=300%2C118&#038;ssl=1\" alt=\"Calling from Main\" width=\"300\" height=\"118\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main3.jpg?resize=300%2C118&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main3.jpg?resize=400%2C158&amp;ssl=1 400w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main3.jpg?w=402&amp;ssl=1 402w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-3092\" class=\"wp-caption-text\"><strong>Calling from Main<\/strong><\/figcaption><\/figure>\n<p>And here is the output:<\/p>\n<figure id=\"attachment_3093\" aria-describedby=\"caption-attachment-3093\" style=\"width: 450px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3093\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/output2-13\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output2.jpg\" data-orig-size=\"590,145\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681895349&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Program Output\" data-image-description=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Program Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output2.jpg\" class=\"wp-image-3093\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output2.jpg?resize=450%2C111&#038;ssl=1\" alt=\"Program Output\" width=\"450\" height=\"111\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output2.jpg?resize=300%2C74&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output2.jpg?w=590&amp;ssl=1 590w\" sizes=\"(max-width: 450px) 100vw, 450px\" \/><\/a><figcaption id=\"caption-attachment-3093\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>An important point should be mentioned here. If you look at <em><strong>line 33<\/strong><\/em>, you can see that I have commented the <em><strong>\u201creturn\u201d<\/strong><\/em> statement. Why? Simply because it doesn\u2019t make sense to have a <em><strong>\u201creturn\u201d<\/strong><\/em> statement inside a <em><strong>defer<\/strong><\/em> block. Since, by definition, the <em><strong>defer<\/strong><\/em> block is called just before returning from the function, allowing a <em><strong>\u201creturn\u201d<\/strong><\/em> inside the block will cause infinite loop!<\/p>\n<h3>The Impact of \u201cpanic\u201d<\/h3>\n<p>We all know that <em><strong>returning<\/strong><\/em> from a function is the <em><strong>normal<\/strong><\/em> function call behavior. And we have seen how the <em><strong>defer<\/strong><\/em> statement is guaranteed to be executed <em><strong>\u201cjust before\u201d<\/strong><\/em> the function returns <em><strong>normally<\/strong><\/em>. What if there is an abnormal situation during program execution? In <em><strong>V<\/strong><\/em> language, the <em><strong>\u201cpanic\u201d<\/strong><\/em> function is used to signal an <em><strong>\u201cabnormal\u201d<\/strong><\/em> situation. In this case, the program terminates abruptly, perhaps printing a stack trace, etc. Will the <em><strong>defer<\/strong><\/em> block be executed in such a situation? The answer is <em><strong>No<\/strong><\/em>. The following function demonstrates this.<\/p>\n<figure id=\"attachment_3095\" aria-describedby=\"caption-attachment-3095\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3095\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/example3-23\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example3.jpg\" data-orig-size=\"564,316\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681896051&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Calling &amp;#8220;panic&amp;#8221;\" data-image-description=\"&lt;p&gt;Calling &amp;#8220;panic&amp;#8221;&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Calling &amp;#8220;panic&amp;#8221;&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example3.jpg\" class=\"wp-image-3095\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example3.jpg?resize=400%2C224&#038;ssl=1\" alt=\"Calling &quot;panic&quot;\" width=\"400\" height=\"224\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example3.jpg?resize=300%2C168&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example3.jpg?resize=352%2C198&amp;ssl=1 352w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example3.jpg?w=564&amp;ssl=1 564w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-3095\" class=\"wp-caption-text\"><strong>Calling &#8220;panic&#8221;<\/strong><\/figcaption><\/figure>\n<p>Depending on the argument passed to the function, the function will return normally, or exit through <em><strong>\u201cpanic\u201d<\/strong><\/em>.<br \/>\nLet us see what happens when we pass <em><strong>\u201cfalse\u201d<\/strong><\/em>, therefore bypassing the <em><strong>\u201cif\u201d<\/strong><\/em> block:<\/p>\n<figure id=\"attachment_3096\" aria-describedby=\"caption-attachment-3096\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3096\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/main4\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main4.jpg\" data-orig-size=\"432,208\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681895647&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"The main Function\" data-image-description=\"&lt;p&gt;The main Function&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The main Function&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main4.jpg\" class=\"size-medium wp-image-3096\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main4.jpg?resize=300%2C144&#038;ssl=1\" alt=\"The main Function\" width=\"300\" height=\"144\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main4.jpg?resize=300%2C144&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main4.jpg?w=432&amp;ssl=1 432w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-3096\" class=\"wp-caption-text\"><strong>The &#8220;main&#8221; Function<\/strong><\/figcaption><\/figure>\n<p>Here is the output:<\/p>\n<figure id=\"attachment_3097\" aria-describedby=\"caption-attachment-3097\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3a.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3097\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/output3a\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3a.jpg\" data-orig-size=\"579,118\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681895482&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Output from Program\" data-image-description=\"&lt;p&gt;Output from Program&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output from Program&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3a.jpg\" class=\"wp-image-3097\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3a.jpg?resize=400%2C82&#038;ssl=1\" alt=\"Output from Program\" width=\"400\" height=\"82\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3a.jpg?resize=300%2C61&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3a.jpg?w=579&amp;ssl=1 579w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-3097\" class=\"wp-caption-text\"><strong>Output from Program<\/strong><\/figcaption><\/figure>\n<p>The <em><strong>defer<\/strong><\/em> block is executed as expected. Let us now pass <em><strong>\u201ctrue\u201d<\/strong><\/em>, triggering a panic:<\/p>\n<figure id=\"attachment_3098\" aria-describedby=\"caption-attachment-3098\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main5.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3098\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/main5\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main5.jpg\" data-orig-size=\"395,243\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681895680&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Passing &amp;#8220;true&amp;#8221; This Time\" data-image-description=\"&lt;p&gt;Passing &amp;#8220;true&amp;#8221; This Time&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Passing &amp;#8220;true&amp;#8221; This Time&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main5.jpg\" class=\"size-medium wp-image-3098\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main5.jpg?resize=300%2C185&#038;ssl=1\" alt=\"Passing &quot;true&quot; This Time\" width=\"300\" height=\"185\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main5.jpg?resize=300%2C185&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/main5.jpg?w=395&amp;ssl=1 395w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-3098\" class=\"wp-caption-text\"><strong>Passing &#8220;true&#8221; This Time<\/strong><\/figcaption><\/figure>\n<p>This is what happens in this case:<\/p>\n<figure id=\"attachment_3099\" aria-describedby=\"caption-attachment-3099\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3b.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3099\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/output3b\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3b.jpg\" data-orig-size=\"1145,241\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;Admin&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;1681895561&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Abnormal Termination\" data-image-description=\"&lt;p&gt;Abnormal Termination&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Abnormal Termination&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3b-1024x216.jpg\" class=\"wp-image-3099\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3b.jpg?resize=550%2C116&#038;ssl=1\" alt=\"Abnormal Termination\" width=\"550\" height=\"116\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3b.jpg?resize=300%2C63&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3b.jpg?resize=1024%2C216&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3b.jpg?resize=768%2C162&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/output3b.jpg?w=1145&amp;ssl=1 1145w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3099\" class=\"wp-caption-text\"><strong>Abnormal Termination<\/strong><\/figcaption><\/figure>\n<p>We can see that the <em><strong>defer<\/strong><\/em> block is not executed.<\/p>\n<p>It is important to stress that the <em><strong>defer<\/strong><\/em> statement is executed when a function <em><strong>\u201creturns normally\u201d<\/strong><\/em> and not when there is an abnormal exit. Nevertheless, it gives us a convenient point to define our <em><strong>\u201cresource clean up\u201d<\/strong><\/em> code.<\/p>\n<p>You can download the source file <a href=\"https:\/\/www.rangakrish.com\/downloads\/DeferExample.v\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>. I have used <em><strong>V 0.3.3<\/strong><\/em> for testing my code.<\/p>\n<p>Have a nice week!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The defer statement in Vlang causes code to be executed when the enclosing function returns. This feature is inspired by Golang, but is slightly more flexible since it allows any block of code to be associated with defer instead of just a function call. defer is used primarily to handle \u201cclean up\u201d logic, such as [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[17,355],"tags":[358,360,359],"class_list":["post-3083","post","type-post","status-publish","format-standard","hentry","category-programming","category-vlang","tag-defer-statement","tag-raii","tag-v-language"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-NJ","jetpack-related-posts":[{"id":3070,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/02\/building-v-language-dll\/","url_meta":{"origin":3083,"position":0},"title":"Building V Language DLL","author":"admin","date":"April 2, 2023","format":false,"excerpt":"The relatively new V Programming Language (Vlang) boasts of some interesting features such as \u201cfast compilation\u201d and runtime performance \u201cas fast as C\u201d, at the same time being a \u201csimple language\u201d. As I normally do with other languages that I learn and use, I decided to see how easy it\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"V Program","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/V-source-267x300.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3153,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/02\/the-sum-type-in-v-programming-language\/","url_meta":{"origin":3083,"position":1},"title":"The Sum Type in V Programming Language","author":"admin","date":"July 2, 2023","format":false,"excerpt":"A Sum data type in V language gives us a convenient way to hold objects of many distinct types (at any time, just one object) and hence is a discriminated union. For example, in C++ we have the variant type. Here is an example of sum type in V language:\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"The \"sum\" Type","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1-300x187.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1-300x187.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1-300x187.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2084,"url":"https:\/\/www.rangakrish.com\/index.php\/2020\/08\/16\/pattern-matching-comparing-elixir-and-mathematica\/","url_meta":{"origin":3083,"position":2},"title":"Pattern Matching: Comparing Elixir and Mathematica","author":"admin","date":"August 16, 2020","format":false,"excerpt":"One of the things I like about Elixir\u00a0is its support for patterns at the core language level, not through library functions as in most other languages. This contributes to writing cleaner code, in my opinion. \u00a0 Another environment that I am familiar with, namely Mathematica, boasts of (arguably) the most\u2026","rel":"","context":"In &quot;Elixir&quot;","block_context":{"text":"Elixir","link":"https:\/\/www.rangakrish.com\/index.php\/category\/elixir\/"},"img":{"alt_text":"Symbolic Expressions","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/pattern-mm.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/pattern-mm.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2020\/08\/pattern-mm.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":739,"url":"https:\/\/www.rangakrish.com\/index.php\/2017\/12\/17\/c17-constexpr-if\/","url_meta":{"origin":3083,"position":3},"title":"C++17: constexpr if","author":"admin","date":"December 17, 2017","format":false,"excerpt":"constexpr if is another nice little feature in C++17, which significantly simplifies the way we code, especially in the context of templates. In this article, I will briefly talk about this feature and look at some examples. constexpr if is a variant of the standard if statement we have had\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Example1","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2017\/12\/Example1-1.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3589,"url":"https:\/\/www.rangakrish.com\/index.php\/2025\/01\/04\/word-transformation-puzzle-part-2-implementation-in-common-lisp\/","url_meta":{"origin":3083,"position":4},"title":"Word Transformation Puzzle &#8211; Part 2: Implementation in Common Lisp","author":"admin","date":"January 4, 2025","format":false,"excerpt":"In the last article\u00a0I discussed an interesting word puzzle and showed how to solve it using Prolog. Here is the problem statement: \u201cYou are given two words of the same length. You have to transform the first word into the second word, by changing only one letter at a time.\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Getting Word Neighbors","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/01\/code1-300x127.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/01\/code1-300x127.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2025\/01\/code1-300x127.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":980,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/07\/08\/book-review-the-modern-c-challenge\/","url_meta":{"origin":3083,"position":5},"title":"Book Review &#8211; The Modern C++ Challenge","author":"admin","date":"July 8, 2018","format":false,"excerpt":"Title: The Modern C++ Challenge Author: Marius Bancilla Publisher: Packt Publishing Year: May 2018\u00a0 This week I have been going through a new book titled The Modern C++ Challenge written by Marius Bancilla. The book is a collection of programming problems (along with suggested solutions) targeting C++ . The title\u2026","rel":"","context":"In &quot;Book Review&quot;","block_context":{"text":"Book Review","link":"https:\/\/www.rangakrish.com\/index.php\/category\/book-review\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/comments?post=3083"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3083\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}