{"id":3153,"date":"2023-07-02T08:32:04","date_gmt":"2023-07-02T03:02:04","guid":{"rendered":"https:\/\/www.rangakrish.com\/?p=3153"},"modified":"2023-07-02T08:32:04","modified_gmt":"2023-07-02T03:02:04","slug":"the-sum-type-in-v-programming-language","status":"publish","type":"post","link":"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/02\/the-sum-type-in-v-programming-language\/","title":{"rendered":"The Sum Type in V Programming Language"},"content":{"rendered":"<p>A <em><strong>S<\/strong><strong>um<\/strong><\/em> data type in <a href=\"https:\/\/github.com\/vlang\/v\/blob\/master\/doc\/docs.md#sum-types\" target=\"_blank\" rel=\"noopener\"><em><strong>V language<\/strong><\/em><\/a> gives us a convenient way to hold objects of many distinct types (at any time, just one object) and hence is a <em><strong>discriminated union<\/strong><\/em>. For example, in <em><strong>C++<\/strong><\/em> we have the <a href=\"https:\/\/www.rangakrish.com\/index.php\/2017\/11\/05\/c17-stdvariant\/\" target=\"_blank\" rel=\"noopener\"><em><strong>variant<\/strong><\/em><\/a> type.<\/p>\n<p>Here is an example of <em><strong>sum<\/strong><\/em> type in <em><strong>V<\/strong><\/em> language:<\/p>\n<figure id=\"attachment_3154\" aria-describedby=\"caption-attachment-3154\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1.jpg?ssl=1\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" data-attachment-id=\"3154\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/02\/the-sum-type-in-v-programming-language\/example1-32\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1.jpg\" data-orig-size=\"896,559\" 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;1688199926&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;sum&amp;#8221; Type\" data-image-description=\"&lt;p&gt;The &amp;#8220;sum&amp;#8221; Type&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;The &amp;#8220;sum&amp;#8221; Type&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1.jpg\" class=\"wp-image-3154\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1.jpg?resize=600%2C374&#038;ssl=1\" alt=\"The &quot;sum&quot; Type\" width=\"600\" height=\"374\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1.jpg?resize=300%2C187&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1.jpg?resize=768%2C479&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example1.jpg?w=896&amp;ssl=1 896w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-3154\" class=\"wp-caption-text\"><strong>The &#8220;sum&#8221; Type<\/strong><\/figcaption><\/figure>\n<p>In the above program, the <em><strong>sum<\/strong><\/em> type <em><strong>Vehicle<\/strong><\/em> stands for the distinct types <em><strong>Car, Van <\/strong><\/em>and<em><strong> Bus<\/strong><\/em>.<\/p>\n<p>The corresponding output is:<\/p>\n<figure id=\"attachment_3155\" aria-describedby=\"caption-attachment-3155\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output1.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3155\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/02\/the-sum-type-in-v-programming-language\/output1-14\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output1.jpg\" data-orig-size=\"595,85\" 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;1688199957&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\/07\/output1.jpg\" class=\"wp-image-3155\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output1.jpg?resize=400%2C57&#038;ssl=1\" alt=\"Program Output\" width=\"400\" height=\"57\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output1.jpg?resize=300%2C43&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output1.jpg?w=595&amp;ssl=1 595w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-3155\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>We can use the expression <em><strong>&lt;var&gt; is &lt;Type&gt;<\/strong><\/em> to determine if a variable currently holds an object of type <em><strong>&lt;Type&gt;<\/strong><\/em>:<\/p>\n<figure id=\"attachment_3156\" aria-describedby=\"caption-attachment-3156\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example2.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3156\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/02\/the-sum-type-in-v-programming-language\/example2-28\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example2.jpg\" data-orig-size=\"542,428\" 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;1688206778&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=\"Using &amp;#8220;is a&amp;#8221; Expression\" data-image-description=\"&lt;p&gt;Using &amp;#8220;is a&amp;#8221; Expression&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using &amp;#8220;is a&amp;#8221; Expression&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example2.jpg\" class=\"wp-image-3156\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example2.jpg?resize=400%2C316&#038;ssl=1\" alt=\"Using &quot;is a&quot; Expression\" width=\"400\" height=\"316\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example2.jpg?resize=300%2C237&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example2.jpg?w=542&amp;ssl=1 542w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-3156\" class=\"wp-caption-text\"><strong>Using &#8220;is a&#8221; Expression<\/strong><\/figcaption><\/figure>\n<p>This prints:<\/p>\n<figure id=\"attachment_3157\" aria-describedby=\"caption-attachment-3157\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3157\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/02\/the-sum-type-in-v-programming-language\/output2-16\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output2.jpg\" data-orig-size=\"578,125\" 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;1688206804&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\" data-image-description=\"&lt;p&gt;Output&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Output&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output2.jpg\" class=\"wp-image-3157\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output2.jpg?resize=400%2C87&#038;ssl=1\" alt=\"Output\" width=\"400\" height=\"87\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output2.jpg?resize=300%2C65&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output2.jpg?w=578&amp;ssl=1 578w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-3157\" class=\"wp-caption-text\"><strong>Output<\/strong><\/figcaption><\/figure>\n<p>Likewise, we can use the expression <em><strong>&lt;var&gt; as &lt;Type&gt;<\/strong><\/em> to dynamically cast the current object held in <em><strong>&lt;var&gt;<\/strong><\/em> to the required <em><strong>&lt;Type&gt;<\/strong><\/em>. As expected, this will trigger a panic if the casting is performed on an invalid type.<\/p>\n<p>This is shown below:<\/p>\n<figure id=\"attachment_3158\" aria-describedby=\"caption-attachment-3158\" style=\"width: 550px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3158\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/02\/the-sum-type-in-v-programming-language\/example3-24\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example3.jpg\" data-orig-size=\"803,398\" 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;1688207991&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=\"Dynamic Casting\" data-image-description=\"&lt;p&gt;Dynamic Casting&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Dynamic Casting&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example3.jpg\" class=\"wp-image-3158\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example3.jpg?resize=550%2C273&#038;ssl=1\" alt=\"Dynamic Casting\" width=\"550\" height=\"273\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example3.jpg?resize=300%2C149&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example3.jpg?resize=768%2C381&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example3.jpg?resize=800%2C398&amp;ssl=1 800w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example3.jpg?w=803&amp;ssl=1 803w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/a><figcaption id=\"caption-attachment-3158\" class=\"wp-caption-text\"><strong>Dynamic Casting<\/strong><\/figcaption><\/figure>\n<p>Here is the program output:<\/p>\n<figure id=\"attachment_3159\" aria-describedby=\"caption-attachment-3159\" style=\"width: 650px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output3.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3159\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/02\/the-sum-type-in-v-programming-language\/output3-11\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output3.jpg\" data-orig-size=\"1132,295\" 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;1688208023&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=\"Result of Dynamic Casting\" data-image-description=\"&lt;p&gt;Result of Dynamic Casting&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Result of Dynamic Casting&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output3-1024x267.jpg\" class=\"wp-image-3159\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output3.jpg?resize=650%2C169&#038;ssl=1\" alt=\"Result of Dynamic Casting\" width=\"650\" height=\"169\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output3.jpg?resize=300%2C78&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output3.jpg?resize=1024%2C267&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output3.jpg?resize=768%2C200&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output3.jpg?w=1132&amp;ssl=1 1132w\" sizes=\"(max-width: 650px) 100vw, 650px\" \/><\/a><figcaption id=\"caption-attachment-3159\" class=\"wp-caption-text\"><strong>Result of Dynamic Casting<\/strong><\/figcaption><\/figure>\n<p>It is often convenient to use the <em><strong>match<\/strong><\/em> expression to work with the individual variant in a <em><strong>sum<\/strong><\/em> type. This is cleaner than using multiple if statements. The <em><strong>print_vehicle<\/strong><\/em> function that we wrote earlier can be rewritten thus:<\/p>\n<figure id=\"attachment_3160\" aria-describedby=\"caption-attachment-3160\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3160\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/02\/the-sum-type-in-v-programming-language\/example4-17\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example4.jpg\" data-orig-size=\"422,493\" 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;1688277487&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=\"Using &amp;#8220;match&amp;#8221; Expression\" data-image-description=\"&lt;p&gt;Using &amp;#8220;match&amp;#8221; Expression&lt;\/p&gt;\n\" data-image-caption=\"&lt;p&gt;Using &amp;#8220;match&amp;#8221; Expression&lt;\/p&gt;\n\" data-large-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example4.jpg\" class=\"wp-image-3160\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example4.jpg?resize=300%2C350&#038;ssl=1\" alt=\"Using &quot;match&quot; Expression\" width=\"300\" height=\"350\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example4.jpg?resize=257%2C300&amp;ssl=1 257w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/example4.jpg?w=422&amp;ssl=1 422w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-3160\" class=\"wp-caption-text\"><strong>Using &#8220;match&#8221; Expression<\/strong><\/figcaption><\/figure>\n<p>The output is as expected:<\/p>\n<figure id=\"attachment_3161\" aria-describedby=\"caption-attachment-3161\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output4.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" data-attachment-id=\"3161\" data-permalink=\"https:\/\/www.rangakrish.com\/index.php\/2023\/07\/02\/the-sum-type-in-v-programming-language\/output4-6\/\" data-orig-file=\"https:\/\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output4.jpg\" data-orig-size=\"593,128\" 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;1688277516&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\/07\/output4.jpg\" class=\"wp-image-3161\" src=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output4.jpg?resize=400%2C86&#038;ssl=1\" alt=\"Program Output\" width=\"400\" height=\"86\" srcset=\"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output4.jpg?resize=300%2C65&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/07\/output4.jpg?w=593&amp;ssl=1 593w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-3161\" class=\"wp-caption-text\"><strong>Program Output<\/strong><\/figcaption><\/figure>\n<p>The <em><strong>match<\/strong><\/em> statement must specify a pattern for all variants of the type, or must include the <em><strong>else<\/strong><\/em> branch to handle what has been left out.<\/p>\n<p>Finally, the <em><strong>sum<\/strong><\/em> type is different from type inheritance as defined in object-oriented programming. In inheritance, when a class <em><strong>\u201cD\u201d<\/strong><\/em> is derived from a base class <em><strong>\u201cB\u201d<\/strong><\/em>, <em><strong>\u201cD\u201d<\/strong> <strong>is a<\/strong> <strong>\u201cB\u201d<\/strong><\/em>. In future, when another class <em><strong>\u201cE\u201d<\/strong><\/em> is derived from <em><strong>\u201cD\u201d<\/strong><\/em>, then that is also a <em><strong>\u201cB\u201d<\/strong><\/em>. In this sense, inheritance is a collection of one-way compatible <em><strong>open<\/strong><\/em> types. That is not the case with a <em><strong>sum<\/strong><\/em> type. It always defines a fixed and distinct set of types.<\/p>\n<p>You can download the example source from <a href=\"https:\/\/www.rangakrish.com\/downloads\/sumtype_example.v\" target=\"_blank\" rel=\"noopener\"><em><strong>here<\/strong><\/em><\/a>.<\/p>\n<p>Have a wonderful weekend!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: In the above program, the [&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_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_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}},"categories":[17,355],"tags":[364,356],"class_list":["post-3153","post","type-post","status-publish","format-standard","hentry","category-programming","category-vlang","tag-sum-type","tag-vlang"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9OLnF-OR","jetpack-related-posts":[{"id":3070,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/02\/building-v-language-dll\/","url_meta":{"origin":3153,"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":3083,"url":"https:\/\/www.rangakrish.com\/index.php\/2023\/04\/20\/defer-statement-in-v-language\/","url_meta":{"origin":3153,"position":1},"title":"Defer Statement in V Language","author":"admin","date":"April 20, 2023","format":false,"excerpt":"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\u2026","rel":"","context":"In &quot;Programming&quot;","block_context":{"text":"Programming","link":"https:\/\/www.rangakrish.com\/index.php\/category\/programming\/"},"img":{"alt_text":"The \"defer\" Statement","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1-300x146.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1-300x146.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2023\/04\/example1-300x146.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":928,"url":"https:\/\/www.rangakrish.com\/index.php\/2018\/05\/27\/calling-go-functions-from-lisp\/","url_meta":{"origin":3153,"position":2},"title":"Calling Go Functions from Lisp","author":"admin","date":"May 27, 2018","format":false,"excerpt":"For the past few weeks I was travelling in the USA and had the pleasure of meeting several friends, some of whom are senior developers and architects. It was interesting to learn that many of them were either using Go language in their daily work, or were passionately endorsing the\u2026","rel":"","context":"In &quot;Golang&quot;","block_context":{"text":"Golang","link":"https:\/\/www.rangakrish.com\/index.php\/category\/golang\/"},"img":{"alt_text":"Part of Header File","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2018\/05\/Header-file.png?resize=525%2C300 1.5x"},"classes":[]},{"id":122,"url":"https:\/\/www.rangakrish.com\/index.php\/2016\/01\/06\/lisp-returning-multiple-values-from-a-function\/","url_meta":{"origin":3153,"position":3},"title":"Lisp: Returning Multiple Values From A Function","author":"admin","date":"January 6, 2016","format":false,"excerpt":"One of the interesting features of Lisp is its support for returning multiple values from a function, without bundling the values in a special container. Languages such as Python and Ruby support multi-value return (although there are subtle differences from Lisp). In C++11, we can use std::make_tuple() and std::tie(). To\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2815,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/05\/29\/setting-up-function-hooks-in-lisp\/","url_meta":{"origin":3153,"position":4},"title":"Setting Up Function Hooks in Lisp","author":"admin","date":"May 29, 2022","format":false,"excerpt":"Lisp is known to be a highly dynamic language, where functions are first-class\u00a0objects. It is possible to define and undefine functions on the fly as well as attach hooks to existing functions. These are in addition to the ability to pass functions as parameters to other functions and returning a\u2026","rel":"","context":"In &quot;LISP&quot;","block_context":{"text":"LISP","link":"https:\/\/www.rangakrish.com\/index.php\/category\/lisp\/"},"img":{"alt_text":"Function Object","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1-300x58.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1-300x58.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/05\/Function-Object1-300x58.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2850,"url":"https:\/\/www.rangakrish.com\/index.php\/2022\/06\/26\/c20-no_unique_address-attribute\/","url_meta":{"origin":3153,"position":5},"title":"C++20 [[no_unique_address]] Attribute","author":"admin","date":"June 26, 2022","format":false,"excerpt":"The [[no_unique_address]] attribute was introduced in C++20 to give a compiler the freedom to optimise memory allocation of a struct\/class when it contains a subobject that does not have any members. The other requirement is that the subobject should not be a static member of the enclosing struct\/class. Let us\u2026","rel":"","context":"In &quot;C++&quot;","block_context":{"text":"C++","link":"https:\/\/www.rangakrish.com\/index.php\/category\/c\/"},"img":{"alt_text":"Empty vs. Non-empty Struct","src":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1-300x213.jpg?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1-300x213.jpg?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.rangakrish.com\/wp-content\/uploads\/2022\/06\/code1-300x213.jpg?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3153","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=3153"}],"version-history":[{"count":0,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/posts\/3153\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/media?parent=3153"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/categories?post=3153"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rangakrish.com\/index.php\/wp-json\/wp\/v2\/tags?post=3153"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}