{"id":203,"date":"2019-07-24T12:13:26","date_gmt":"2019-07-24T18:13:26","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=203"},"modified":"2019-09-28T12:04:53","modified_gmt":"2019-09-28T18:04:53","slug":"making-platform-specific-error-codes-generic","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/07\/24\/making-platform-specific-error-codes-generic\/","title":{"rendered":"Making platform specific error codes generic"},"content":{"rendered":"\n<p>A few threads on the forums have commented that the URLConnection isnt quite as easy to use as many might expect. In particular<a href=\"https:\/\/forum.xojo.com\/conversation\/post\/445458\"> there are comments <\/a>about having to know what error codes a platform might return makes it harder to use than it should be.<\/p>\n\n\n\n<p>Normally Xojo hides this level of detail from us.<\/p>\n\n\n\n<p>I was thinking about this problem and have come up with something of a solution that makes it possible to both know the specific error code and yet still write code that is portable.<\/p>\n\n\n\n<p>My solution relies on the fact that, at compile time, numeric constants will take on one of many possible values if you set up platform specific versions of a numeric constant. Its possible to set up a constant with a specific value for macOS, Windows, Linux, and iOS (as well as a couple that are legacy types) as follows :<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"186\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2019\/07\/Screenshot-2019-07-17-at-12.22.16-PM.png?resize=525%2C186&#038;ssl=1\" alt=\"\" class=\"wp-image-204\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2019\/07\/Screenshot-2019-07-17-at-12.22.16-PM.png?w=761&amp;ssl=1 761w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2019\/07\/Screenshot-2019-07-17-at-12.22.16-PM.png?resize=300%2C106&amp;ssl=1 300w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p>If you were to compile this code on macOS the Foo constant would have the value 1, on Windows it would be 2 and so on. The nice thing is that code could simply use the symbolic constant Foo instead of having to rely on the specific value. Instead of writing<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ is someVariable = Foo on macOS ?\nif someVariable = 1 then\n   \/\/ do whatever should be done \nend if<\/pre>\n\n\n\n<p>you could, and probably should write<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ is someVariable = Foo on macOS ?\nif someVariable = Foo then\n   \/\/ do whatever should be done \nend if<\/pre>\n\n\n\n<p>This is has the added benefit of making your code more robust since a simple change to a constant is all thats required to instead of finding all the magic number 1&#8217;s everywhere. But how does this help us to making generic platform specific error codes (which I admit is a bit of an oxymoron ?)<\/p>\n\n\n\n<p>An enumerated value can be set from one of several possible sources. It can have no specific value assigned, have a literal value, an enumerated value from another enum, or a constant.<\/p>\n\n\n\n<p>IF Enum2 is is defined as<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Public Enum Enum2\n  value1 = 10\nEnd Enum<\/pre>\n\n\n\n<p>Enum1 can be defined as<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"525\" height=\"186\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2019\/07\/Screenshot-2019-07-17-at-12.39.26-PM.png?resize=525%2C186&#038;ssl=1\" alt=\"\" class=\"wp-image-205\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2019\/07\/Screenshot-2019-07-17-at-12.39.26-PM.png?w=740&amp;ssl=1 740w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2019\/07\/Screenshot-2019-07-17-at-12.39.26-PM.png?resize=300%2C106&amp;ssl=1 300w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted\">Public Enum Enum1\n  value1 \/\/ no specific value assigned\n  value2 = 99999\n  value3 = enum2.value1\n  value4 = kConst\nEnd Enum<\/pre>\n\n\n\n<p>That we can use a constant is especially notable as we just saw we can make a constant platform specific. So its possible to have an enumerated value that takes on the value of a platform specific version of a constant (but be careful with this as you would not want to have many enumerated values with the same value as that makes them harder to use)<\/p>\n\n\n\n<p>If we defined our enum as<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Public Enum kDemoEnum\n  value1 = kConstant\nEnd Enum<\/pre>\n\n\n\n<p>and the constant as <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Public Const kConstant as Number = -1\n  OS X, default language, 1\n  Windows, default language, 2\nend Const  <\/pre>\n\n\n\n<p>when we compiled on macOS the value for <em>kDemoEnum.value1 <\/em>would be 1, on Windows 2 and on any other it would be -1 (the default for the enum)<\/p>\n\n\n\n<p>So now you can make enumerations that give you the flexibility of named values without having to know the specific values AND a generic set of enumerated values that reflect platform specific values taken from constants.<\/p>\n\n\n\n<p>Use carefully.<\/p>\n\n\n\n<p>UPDATE ! &#8211; <a href=\"http:\/\/great-white-software.com\/miscellaneous\/platform%20specific%20http%20error%20codes.zip\">here&#8217;s an example<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few threads on the forums have commented that the URLConnection isnt quite as easy to use as many might expect. In particular there are comments about having to know what error codes a platform might return makes it harder to use than it should be. Normally Xojo hides this level of detail from us. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/07\/24\/making-platform-specific-error-codes-generic\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Making platform specific error codes generic&#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":[27,6,3],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-advanced-techniques","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\/203","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=203"}],"version-history":[{"count":2,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":336,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/203\/revisions\/336"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}