{"id":108,"date":"2019-06-25T17:25:31","date_gmt":"2019-06-25T17:25:31","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=108"},"modified":"2019-06-21T00:45:10","modified_gmt":"2019-06-21T00:45:10","slug":"let-the-compiler-help-you","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/06\/25\/let-the-compiler-help-you\/","title":{"rendered":"Let the compiler help you"},"content":{"rendered":"\n<p>An interesting question posed by a relatively new user came up on the forums. It had to do with how the Array() function worked and how he could force it to return an array of variants to call his method.<\/p>\n\n\n\n<p>His method was defined like<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">BuildMenu( base as menuitem, params() as variant)<\/pre>\n\n\n\n<p>And he would call it like<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Result = BuildMenu (base, Array (\"Run\", False, \"Edit\", \"Rename\", \"Delete\") )<\/pre>\n\n\n\n<p>and this would build up a menu from the base item and append menu items to it. Those new menu items were configured by the array of variants <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>&#8216;False&#8217; means the previous menu item is disabled (&#8216;True&#8217; means it is enabled, but is optional and assumed)<\/p><p>Because the boolean elements are optional, it is possible to make a call using only string elements, such as:<\/p><p>Result = BuildMenu (base, Array (&#8220;Run&#8221;, &#8220;Edit&#8221;, &#8220;Rename&#8221;, &#8220;Delete&#8221;) )<\/p><p>But when I do this, I get the following compile error:<\/p><\/blockquote>\n\n\n\n<p>So he&#8217;s trying to figure out how to force Array to return variants ALL the time. Which is the wrong question to ask in many ways.<\/p>\n\n\n\n<p>Using an array of variants means the compiler cannot help you out and tell you when you are trying to assign the wrong kind of value to another. <\/p>\n\n\n\n<p>Normally if you did something like <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">dim foo as integer\nfoo = \"123\"<\/pre>\n\n\n\n<p>the compiler will warn you about this. But if you change this simple code to :<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">dim foo as variant\nfoo = \"123<\/pre>\n\n\n\n<p>you will no longer get a compile error. And depending on how you use &#8220;foo&#8221; you may, or may not, get some kind of runtime error.<\/p>\n\n\n\n<p>This is why some of the long time users of Xojo suggest <a href=\"https:\/\/www.bkeeneybriefs.com\/2019\/06\/dont-overuse-variants\/\">avoiding variants<\/a> unless you absolutely cannot do something any other way.<\/p>\n\n\n\n<p>Variants make it possible to bypass and avoid type checking by the compiler. What this means is that by using a variant you are possibly only finding out about errors at runtime because the compiler cannot do its normal helpful type checking to make sure you catch errors early on.<\/p>\n\n\n\n<p>So in this particular thread on the forums the right question isn&#8217;t &#8220;how to force the array() function to always return a variant array&#8221; but &#8220;how to design this particular API so variants are not required&#8221;. And that is certainly possible since Xojo supports variable argument lists, optional parameters and other features that retain strong type checking at compile time AND still allow for a good degree of flexibility in designing an API.<\/p>\n\n\n\n<p>And I made a suggestion on that thread :<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>Result = BuildMenu (base, BuildItem( &#8220;Run&#8221;, false), BuildItem(&#8220;edit&#8221;), BuildItem(&#8220;Rename&#8221;), BuildItem(&#8220;Delete&#8221;))<br> with buildmenu defined as<\/p><p>Sub BuildMenu(base as menuitem, paramarray items as menuitem)<br>         \/\/ just append the items passed to the base<br>         for i as integer = 0 to items.ubound<br>                base.append items(i)<br>         next<br> end sub<\/p><p>and BuildItem as<\/p><p>Function BuildItem(name as string, enabled as boolean = false)<br>      dim item as new menuitem(name)<br>     item.enabled = enabled<br>     return item<br> end function<\/p><\/blockquote>\n\n\n\n<p>Avoid variants where you can and design an API that permits the compiler to help you out at compile time. The earlier you catch possible bugs the easier, and less costly, they are to fix.<\/p>\n\n\n\n<p>The compiler can help you IF you let it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An interesting question posed by a relatively new user came up on the forums. It had to do with how the Array() function worked and how he could force it to return an array of variants to call his method. His method was defined like BuildMenu( base as menuitem, params() as variant) And he would &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/06\/25\/let-the-compiler-help-you\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Let the compiler help you&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[16,17,6,3],"class_list":["post-108","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-best-practices","tag-compiler","tag-tips","tag-xojo"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=108"}],"version-history":[{"count":3,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/108\/revisions"}],"predecessor-version":[{"id":131,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/108\/revisions\/131"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}